How to dynamically rebind JQuery Objects

前端 未结 2 1633
遇见更好的自我
遇见更好的自我 2021-01-19 23:28

I am making a module for the Joomla! page of a client that uses Ajax to render different queries to the database. The result of these queries is that I regenerate the entire

2条回答
  •  有刺的猬
    2021-01-19 23:48

    Bind the event handler on a parent element and use event delegation to handle the events. By binding the event handler on a parent element you don't need to bind it on all the child elements.

    /HTML/

    /Javascript/

    // use this
    $("#container").on("click", ".click", function(){
        alert(this.id + ": I've been clicked.");
    });
    
    // don't use this
    $(".click").on("click", function(){
        alert(this.id + ": I've been clicked.");
    });
    

提交回复
热议问题