简体   繁体   中英

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

是否存在以下语法:

$$('.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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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