最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery on('click') handler for multiple elements referenced by vars - Stack Overflow

programmeradmin4浏览0评论

N.B. I'm aware that I add ids and bine these in a selector e.g. "#myDiv1,#myDiv2" so please refrain from suggesting this as it does not relate to my question.

Is there a way to 'chain' the vars below together in one on() declaration maybe as an array or something?

var myDiv1 = $('<div>Something here</div>');
var myDiv2 = $('<div>Something else here</div>');

myDiv1.on('click', function(){ doSomething();});
myDiv2.on('click', function(){ doSomething();});

I have a bunch of vars that I need to do some broad tracking of mouse events and it feels messy setting them up individually like the above example.

N.B. I'm aware that I add ids and bine these in a selector e.g. "#myDiv1,#myDiv2" so please refrain from suggesting this as it does not relate to my question.

Is there a way to 'chain' the vars below together in one on() declaration maybe as an array or something?

var myDiv1 = $('<div>Something here</div>');
var myDiv2 = $('<div>Something else here</div>');

myDiv1.on('click', function(){ doSomething();});
myDiv2.on('click', function(){ doSomething();});

I have a bunch of vars that I need to do some broad tracking of mouse events and it feels messy setting them up individually like the above example.

Share Improve this question asked Nov 28, 2013 at 23:32 RobRob 10.2k5 gold badges63 silver badges93 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7
  • You can pass an array of DOM elements to the jQuery function:

    $([
        myDiv1.get(0), 
        myDiv2.get(0)
    ]).on('click', function(){ doSomething();});
    

    jsFiddle Demo

  • Another possible way is to use the .add() method:

    myDiv1.add(myDiv2).on('click', function(){ doSomething();});
    

    jsFiddle Demo

  • Put them in an array, loop through them and attach the same handler. I made the example with ES5 forEach, but feel free to use a simple for loop or $.each.

    [cucc, gomb].forEach(function (el) {
        el.on('click', handler);
    });
    

    jsFiddle Demo

  • If they have a mon ancestor, put a mon class on them and use event delegation. Depending on the number of your elements, this could be the best solution performance-wise, because you only have to attach one handler to the mon ancestor.

    $('.ancestor').on('click', '.mon', handler);
    

    jsFiddle Demo

Adding to Kapa's answer. If you need to bind multiple elements to which have been added, prepended, appended, loaded with ajax or javascript.

Loop through multiple classes and create bindings "for each" element


var thisButton = '.this-button', //This element was added by js
    thatButton = '.that-button', //That element was added by ajax
    thesebuttons = [thisButton, thatButton];

    thesebuttons.forEach(function (button) {
        $(document).on('click touchend', button, function (e) {
        // 'e' or event, unment to prevent button's default event
        // e.preventDefault(); 

        // Now do your function stuff

        });
    });


发布评论

评论列表(0)

  1. 暂无评论