Home > Web Front-end > JS Tutorial > 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

黄舟
Release: 2017-06-27 10:11:40
Original
1786 people have browsed it

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");    });
Copy after login

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>";
Copy after login

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");
    });
Copy after login

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");
    });
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template