Home > Article > Web Front-end > jquery’s solution to the invalid click event in the js code generated later
The jquery version used: jquery-3.1.1
The click event written directly in the page is valid:
<a class="deleteCls" beanId="${bean.id }" href="javascript:void(0)">删除</a> $("a.deleteCls").click(function() { alert("del"); });
But the code generated in js:
str += "<td><a class='deleteCls' beanId='" + item.id + "' href='javascript:void(0)'>删除</a></td>";
click It failed. After checking the information, it was said that the code was generated later. jquery did not bind the click event. I did not have an in-depth understanding of the details.
The following is the solution:
Modify
$("a.deleteCls").click(function() { alert("del"); });
to (tableList is the id of the table table, and the generated code is in the table tag):
$("#tableList").on("click","a.deleteCls",function(){ alert("del"); });
Solved, now the click event of the generated code can also respond!
The above is the detailed content of jquery’s solution to the invalid click event in the js code generated later. For more information, please follow other related articles on the PHP Chinese website!