简体   繁体   English

事件监听器循环

[英]Event listener loop

How do I add a listener for hovering over div tags like this:如何添加一个侦听器以将鼠标悬停在 div 标签上,如下所示:

btn1 btn1 btn2 btn2 btn3 btn3 btn4 btn4

I want to add a listener that loops through them like I show below and then applies a function if it has mouseover .我想添加一个像下面显示的那样循环遍历它们的侦听器,然后如果它有mouseover则应用一个函数。

function listen() {
 for (i=1;i<=10;i++) {
  wat = document.getElementById('btn'+i);  
  wat.addEventListener('mouseover',functionwat,false );
 }
}

I have this and its not working, and yes it is calling the function listen() , because I added an alert thing in there to make sure its working correctly, and functionwat works right too.我有这个但它不工作,是的,它正在调用函数listen() ,因为我在其中添加了一个警报以确保它正常工作,并且functionwat也可以正常工作。 Any idea what I'm doing wrong?知道我做错了什么吗?

What browser are you using?你使用的是什么浏览器? Registering event handlers is different browser to browser.注册事件处理程序是不同的浏览器。 PPK has some good discussion of browser events here . PPK在此处对浏览器事件进行了一些很好的讨论。

In short, this is the cross-browser code for adding a handler.简而言之,这是添加处理程序的跨浏览器代码。

function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener)
        obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
        obj.attachEvent('on'+evt,fn);
}

Now you can attach the event with现在您可以将事件附加到

function listen() {
    for (i=1;i<=10;i++) {
        wat = document.getElementById('btn'+i);     
        addEventSimple(wat, 'mouseenter', functionwat);
    }
}

Instead of looping for each item and attaching events, look into implementing event delegation.与其循环每个项目并附加事件,不如研究实现事件委托。 As it relates your situation, let assume you use jQuery and your buttons' markup is as followed:由于它与您的情况有关,假设您使用 jQuery 并且按钮的标记如下:

<div id="btnList">
<button id="btn1">btn1</button>
<button id="btn2">btn2</button>
<button id="btn3">btn3</button>
<button id="btn4">btn4</button>
</div>

JavaScript: JavaScript:

$(document).ready(function()
{
   $("#btnList button").bind(
          "mouseenter mouseleave",
          function(e) {
             //do something based on target/id 
             alert(this.id);
          });
});

It seems that you might be somewhat messy with your variables.您的变量似乎有些混乱。 For instance, you do not use var to declare i , so it might end up in the global namespace.例如,您不使用var来声明i ,因此它可能最终位于全局命名空间中。 Following this, are you sure functionwat is really a function at the time listen() executes?在此之后,您确定在listen()执行时functionwat真的是一个函数吗?

You could check that by;您可以通过以下方式检查;

function listen() {

   if(typeof functionwat !== "function") {
      alert("functionwat is not a function, but a " + typeof functionwat);
   }

   for (var i = 1; i <= 10; ++i) {
      wat = document.getElementById("btn"+i);  
      wat.addEventListener("mouseover", functionwat, false);
   }
}

David,大卫,

You're not having any luck because, I am almost positive you are using a browser which is not IE.您没有任何运气,因为我几乎可以肯定您使用的是不是 IE 的浏览器。 Your events will not fire in a non-IE browser because the event "mouseenter" is only exposed in IE.您的事件不会在非 IE 浏览器中触发,因为事件“mouseenter”仅在 IE 中公开。 To make it work, you need to change "mouseenter" to use "mouseover".要使其工作,您需要将“mouseenter”更改为使用“mouseover”。

function listen() {
    for (i=1;i<=10;i++) {
        wat = document.getElementById('btn'+i);     
        addEventSimple(wat, 'mouseenter', functionwat);
    }
}

to

function listen() {
    for (i=1;i<=10;i++) {
        wat = document.getElementById('btn'+i);
        if(wat) { addEventSimple(wat, 'mouseover', functionwat); }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM