applying the same .click event to two different divs

前端 未结 6 1384
孤独总比滥情好
孤独总比滥情好 2021-02-09 07:19

i have two different divs (code was simplified, this is not the actual code.)

6条回答
  •  一生所求
    2021-02-09 08:09

    You have two options, either you could give both of those divs a class and then just run the code:

    $(".myclass").click(function() {
        DoWork();
    });
    

    Or you could define a function like:

    function hookClick(arr, handler)  {
        foreach (var i in arr) {
            $(i).click(handler);
        }
    }
    

    then run it using

    hookClick(["#div1", "#div2"], function() { /*do sth cool })
    

提交回复
热议问题