Prototype - click event by element class name

别说谁变了你拦得住时间么 提交于 2020-01-01 04:24:29

问题


I am new to the prototype framework and am trying something really simple and failing. I am trying to respond to a click event on a button like so:

$$('.btn').observe('click', respond);
function respond(event) {
    alert("hello");
}

Why isn't this working? Please help!


回答1:


Unlike jQuery, handing selectors with multiple results in Prototype works a little differently. You need to handle each selected result separately using .each().

$$('.btn').each(function(element) {
    element.observe('click', respond);
})

This is one of the reasons I moved over to jQuery. The other reason: knowing jQuery is marketable and knowing Prototype is not.




回答2:


Can be also be done with a single-liner, as someone already suggested in a comment:

$$('.btn').invoke('observe', 'click', respond);


来源:https://stackoverflow.com/questions/3945663/prototype-click-event-by-element-class-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!