开发者

MooTools - Detect the Array Index of the Class Clicked

开发者 https://www.devze.com 2023-02-19 01:59 出处:网络
Can anyone tell me how I can detect the array index of the class clicked. So if I had: HTML   <span class=\"test\">first</span>

Can anyone tell me how I can detect the array index of the class clicked.

So if I had:

HTML  

<span class="test">first</span>
<span class="test开发者_开发知识库">second</span>
<span class="test">third</span>

MooTools JS

$$('.test').addEvent('click', function(event){
       // alert(this array index);
});

Any help would be great, Thanks!


very simple - don't .addEvent, which will do the .each for you, but write the .each yourself instead, it supports element, index:

$$('.test').each(function(el, index) {
    el.addEvent('click', function(event){
       alert(index); // 0, 1, 2
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号