adding jQuery click events to dynamically added content

后端 未结 4 609
礼貌的吻别
礼貌的吻别 2020-11-28 13:33

I have a table with multiple rows and columns populated by php and mySQL. For some of the td\'s I\'m adding jQuery click-events in the document.ready function to let the use

4条回答
  •  时光取名叫无心
    2020-11-28 14:29

    You want to use live events, which were introduced in 1.3.

    $("tr.clickable").live("click", function() {
       //add input fields
    });
    
    $("span#addNewRow").live("click", function() {
       $("table").append(' ... ')
    });
    

    UPDATE: Note that as of jQuery 1.7, live() is deprecated. Use on() instead. And in some cases, delegate() may be a better choice. See the comments below.

    Example of how to use .on():

    $("table").on("click", "tr.clickable", function() {
       //add input fields
    });
    

提交回复
热议问题