2017-10-17 190 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