2010-02-05 29 views

回答

22

$$可以通過CSS選擇器檢索元素,通過period notation.包括類:

$$('.myClass'); // array with all elements that have class "myClass" 

要回答你的問題,Event.observeobserve「靜態」版本(所有意圖和目的)。爲方便原型automagically makes .observe available off of all DOM elements(獲取與任何$$$):

例子:

// get one item by id with $ and attach an event listener: 
$('myId').observe(eventName, handler); 

// get many items by class with $$ and attach an event listener: 
$$('.myClass').each(function(element) { 
    element.observe(eventName, handler); 
}); 

// or shorter: 
$$('.myClass').invoke('observe', eventName, handler); 
+0

它不是IE – Miral 2011-05-05 16:26:52

+0

只能夠觀察單個元素的事件處理問題(甚至與'$ $('...')。each()'上面的方法)是所有的元素都需要在附加事件時存在,所以你實際上並沒有動態地監聽那個類的事件。 – JMTyler 2014-07-01 20:33:36