Jquery: event listener for array of elements

前端 未结 5 1273
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 13:49

I am populating an array with elements using the .each() method and $(this) selector.

(function($){

    var elements = new Array()         


        
5条回答
  •  无人共我
    2021-01-20 13:58

    Use jQuery's map function to join your array elements into a single combined jQuery object.
    Then attach jQuery events to this object (using "on()" in this example).

    var elements = [$('#blah'), $('#blup'), $('#gaga')];  //basically what you are starting with
    
    var combiObj = $.map(elements, function(el){return el.get()});
    $(combiObj).on('click', function(ev){
    	console.log('EVENT TRIGGERED');
    });
    button{
      display: block;
      margin: 10px;
    }
    
    
    
    

提交回复
热议问题