Prototype - click event by element class name

前端 未结 2 1709
傲寒
傲寒 2021-02-13 18:41

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\')         


        
相关标签:
2条回答
  • 2021-02-13 18:50

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

    $$('.btn').invoke('observe', 'click', respond);
    
    0 讨论(0)
  • 2021-02-13 19:00

    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.

    0 讨论(0)
提交回复
热议问题