Home  >  Article  >  Web Front-end  >  jquery's solution to the invalid click event in the js code generated later

jquery's solution to the invalid click event in the js code generated later

黄舟
黄舟Original
2017-06-27 10:11:401754browse

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=&#39;deleteCls&#39; beanId=&#39;" + item.id + "&#39; href=&#39;javascript:void(0)&#39;>删除</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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn