2017-10-17 71 views
0

這是一個很大的一段代碼鼠標事件,但如果你將沉醉我:爲什麼不工作

<html> 
    <head> 
    <style> 
     .mobile {position: absolute;} 
    </style> 
    <script> 
     var images = new Array(); 
     image['Key1']='image1.png'; 
     image['Key2']='image2.png'; 
     image['Key3']='image3.png'; 
     image['Key4']='image4.png'; 
     image['Key5']='image5.png'; 

     function createAll() 
     { 
     tag = document.getElementById("canvas"); 
     for(var key in image) 
     { 
      var r = key; 
      while(r.indexOf('_')>-1) 
      { 
      r=r.replace('_',' '); 
      } 

      let t = document.createElement("p"); 
      t.id=r; 
      t.className="mobile" 
      t.xVel = Math.floor(Math.random()*50-25); 
      t.yVel = Math.floor(Math.random()*50-25); 
      t.xPos = Math.floor(Math.random()*1000)-60; 
      t.style.left= t.xPos; 
      t.onclick="clickTag('"+r+"')"; 
      t.yPos=Math.floor(Math.random()*600)-42; 
      ////THIS IS WHERE THE EVENT IS ADDED//// 
      t.addEventListener("onmousedown", function(){clickTag(t);}); 
      ////THIS IS WHERE THE EVENT IS ADDED//// 
      t.style.top=t.yPos; 
      var i = document.createElement("img"); 
      i.src=image[key]; 
      var s = document.createElement("span"); 
      tag.appendChild(t); 
      t.appendChild(i); 
      t.appendChild(s); 
      setTimeout(function() {step(t);},200); 
     } 
     } 

     function restartMe(tag) 
     { 
     var x = Math.floor(Math.random()*1000); 
     var y = Math.floor(Math.random()*600); 
     var xVel = Math.floor(Math.random()*50-25); 
     var yVel = Math.floor(Math.random()*50-25); 

     var r = Math.random(); 
     if(r<.25)//left wall 
     { 
      x=-59; 
      xVel = Math.floor(Math.random()*10); 
     } 
     else if(r<.5)//right wall 
     { 
      x=1059; 
      xVel = -Math.floor(Math.random()*10); 
     } 
     else if(r<.75)//top wall 
     { 
      y=-41; 
      yVel = Math.floor(Math.random()*10); 
     } 
     else//bottom wall 
     { 
      y=641; 
      yVel = -Math.floor(Math.random()*10); 
     } 

     tag.xPos = x; 
     tag.style.left=x; 
     tag.yPos = y; 
     tag.style.top=y; 
     tag.style.xVel=xVel; 
     tag.style.yVel=yVel; 
     let t = tag; 
     setTimeout(function() {step(t);},200); 
     } 

     function step(tag) 
     { 
     var x = tag.xPos; 
     var y = tag.yPos; 
     var dx = tag.xVel; 
     var dy = tag.yVel; 
     x+=dx; 
     y+=dy; 

     let t = tag; 
     if(x<-60 || x>1060 || y<-42 || y>642) 
     { 
      x=-500; 
      y=-500; 
      tag.xPos=x; 
      tag.yPos=y; 
      tag.style.left=x; 
      tag.style.top=y; 
      setTimeout(function() {restartMe(t);},1000); 
      return; 
     } 

     tag.xPos=x; 
     tag.yPos=y; 
     tag.style.left=x; 
     tag.style.top=y; 
     setTimeout(function() {step(t);},200); 
     } 

     function startGame() 
     { 
     var tag = document.getElementById("game"); 
     target = Object.keys(image)[Math.floor(Math.random()*Object.keys(image).length)]; 
     var r = target; 
     while(r.indexOf('_')>-1) 
     { 
      r=r.replace('_',' '); 
     } 
     target=r; 
     tag.innerHTML="Look for the "+target; 
     } 

     function clickTag(id) 
     { 
     ////HERE IS WHERE THE MOUSE EVENT SHOULD EXECUTE//// 
     if(id===target) 
     { 
      startGame(); 
     } 
     var tag = document.getElementById("output"); 
     tag.innerHTML="No, that is the "+id; 
     } 
    </script> 
    </head> 
    <body onload="createAll();startGame()"> 
    <h2>What do you see?</h2> 
    <p id="game"></p> 
    <p id="output"></p> 
    <p id="canvas" class="black" width="1000" height="600"></p> 
    </body> 
</html> 

好,這」辦下來。我從數組中的幾個圖像文件名開始,並使用標識圖像的鍵。加載頁面時,我會經歷創建一組包含圖像和單擊事件的可移動p標記的過程。每個標籤有一個超時,然後它們在屏幕上移動。當它們中的一個到達邊界時,它等待一秒鐘,然後從一個邊界再次開始。這部分工作正常,但鼠標事件不。

我一直在尋找鼠標事件,當我點擊其中的一件事情,但沒有發生。我曾嘗試將事件放在p和img上。我試過了onmousedown,onmouseup,onclick等的變體,似乎沒有任何工作。我可能錯過了一些明顯的東西,所以我會欣賞第二組眼睛。

+0

becuase addEventListener在事件名稱中不使用「on」 – epascarello

回答

2

第一的addEventListener不會在字符串中使用「上」

t.addEventListener("mousedown", ... 

現在你正確地添加其他所有事件和調用關閉與超時,但你打造單擊事件錯誤。

t.onclick="clickTag('"+r+"')"; 

這是給事件監聽器分配一個字符串,它沒有綁定一個函數調用。

t.onclick= function() { clickTag(r); }; 
+0

啊。是。那樣做了。謝謝 – BSD

0

還有的createAll只有一個實例,在你的榜樣,但它作爲一個函數聲明所示,所以你永遠不會真正執行createAll(),所以它不是使您的事件偵聽器。

嘗試更改createOne()createAll()

<body onload="createOne();startGame()"> 
+1

謝謝。這是一個錯字。我現在修好了。 – BSD