当前位置:首页 » 《随便一记》 » 正文

jQuery添加元素后不执行原有事件的方法_hermit_j的博客

27 人参与  2022年05月23日 08:19  分类 : 《随便一记》  评论

点击全文阅读


我们先来看下我的错误代码

html:

        <table border="1" id="best">
			<tr>
				<td>
					<button class="change">修改</button>
				</td>				
			</tr>
			<tr>
				<td>
					<button class="del">删除</button>
				</td>				
			</tr>
			<tr class="last"><td><button class="add">添加</button></td></tr>
		</table>

js:

$(".add").click(function(){
		var newYuansu = $("<tr><td><button class='del'>删除</button></td></tr>");
		$(".last").before(newYuansu);
})
$(".del").click(function(){
        $(this).parents("tr").remove();
})

而此时使用jQuery新加元素,新元素元素不会执行一部分原有的事件函数

博主是学生,也只是研究了一种方法:

$("#best").on("click",".del",function(){
		$(this).parents("tr").remove();
})

把原本的click事件这样写,就可在新加的元素使用,非常好用~


点击全文阅读


本文链接:http://zhangshiyu.com/post/40580.html

元素  删除  事件  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1