jQuery $(“.class”).click(); - multiple elements, click event once

前端 未结 18 1659
借酒劲吻你
借酒劲吻你 2020-11-29 00:01

I have multiple classes on a page of the same name. I then have a .click() event in my JS. What I want to happen is the click event only happen once, regardless of multiple

18条回答
  •  迷失自我
    2020-11-29 00:53

    I tried it myself in my project.

    All my rows in a table have a class "contact":

    All my rows in a table have a class

    My code looks like this:

    var contactManager = {};
    
    contactManager.showEditTools = function(row) {
      console.debug(row);
    }
    
    $('.contact').click(function(event){
      console.log("this should appear just once!");
      alert("I hope this will appear just once!");
      contactManager.showEditTools(event);
    });
    

    And I was first scared to see my whole rows as a result in the Firebug console when I executed the code:

    Firebug console screenshot after code execution

    But then I realized that the click event was not fired, because no alert-dialog appeared. Firebug shows you just the elements which are affected by the click overiding. So there is nothing to worry.

提交回复
热议问题