开发者

how to attach onclick event to an array of elements (mootools)

开发者 https://www.devze.com 2022-12-24 17:31 出处:网络
Is there a syntax for开发者_如何学编程 the following thing: $$(\'.a-lot-of-elems\').addEvent(\'someevent\',somefunction);

Is there a syntax for开发者_如何学编程 the following thing:

$$('.a-lot-of-elems').addEvent('someevent',somefunction);


First off - the following will work just fine.

$$(selector).addEvents({
    click: fn
});

Don't use for, to iterate through an element collection, use each instead:

$$(selector).each(function(el){
    el.addEvents({
        click: fn
    });
});

Here's a working example: http://jsfiddle.net/EPYBx/


You are just missing the event type.

var someFunction = function(e) {
  alert('clicked!');
}

$$('.a-lot-of-elems').addEvent('click', somefunction);

Alternatively, you can use

$$('.a-lot-of-elems').addEvent('click', function(e) {
  alert('clicked!');
});


something like

var elements = $$('.a-lot-of-elems')
for(var i = 0 ; i < elements.length ; i = i + 1)
{
  elements[i].addEvent(somefunction);
}

should do ya!

0

精彩评论

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

关注公众号