applying the same .click event to two different divs

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

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

6条回答
  •  日久生厌
    2021-02-09 07:59

    Assign a class to your div's, and then assign the click event using the class like this:

    Then

    $('.clickable').click(function()
    {
        DoWork();
    });
    

    You could also list the id's like this:

    $("#test, #funt").click(DoWork);
    

    I usually try to use classes because it's cleaner when you're dealing with multiple divs - you don't have to list the ID of each.

提交回复
热议问题