how to trigger click event on dynamically created element in jquery

后端 未结 4 899
清酒与你
清酒与你 2021-01-12 02:18
if($(\'#term\').children().length == 0){
        $(\"#term\").append(\"
    \"); var i; for(i=1;
    4条回答
    •  无人共我
      2021-01-12 02:34

      do something like:

      $("#term").append("
        "); var i; for(i=1;i<=3;i++){ var liDynamic = "Term "+i; var liId = "Term"+i; var $li = $("
      • ", { "id" : liId, "class" : 'listDynamic' }).html(liDynamic).click(function() { alert("clicked:" + this.id); }); $("#ulDynamic").append($li); } $("#ulDynamic").find("li:first").trigger("click");
      • Demo :: jsFiddle

      提交回复
      热议问题