[转]jquery append 动态添加的元素事件on 不起作用的解决方案 - CQ_LQJ

WBOY
Release: 2016-05-20 13:49:08
Original
1021 people have browsed it


Copy after login

用jquery添加新元素很容易,面对jquery append 动态添加的元素事件on 不起作用我们该如何解决呢?on方法中要先找到原选择器(如例.info),再找到动态添加的选择器(如列.delete)。


         
Copy after login

具体不多说了,请看下面的代码吧。

 

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> html> head> meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> title>测试title> script type="text/javascript" src="../resources/js/jquery-1.11.1.min.js">script> script type="text/javascript"> $(function(){ //动态添加 $(".add").on("click",function(){ console.log("进来了"); $(".info").append('

单击我来删除我

'); }); //对动态添加的元素添加事件-删除 $(".info").on("click",".delete",function(){ console.log("进来了Delete!"); $(this).remove(); }); }); script> head> body> h2 class='add'>单击我添加动态元素h2> div class="info">div> body> html>
Copy after login

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!