论坛首页 Web前端技术论坛

jquery clone后事件监听灾难

浏览 6130 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-08-10  

最近在写一个项目,出现了瓶颈。js没学好。分离出一个demo测试了一下。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MyCalendar</title>
<script type="text/javascript" src="jquery-1.6.min.js"></script>
<script type="text/javascript">
	$(function() {
		$("#btn").click(function() {
			$("#hide_child .child").clone().show().appendTo("#parent");
		});
		$(".delete").click(function() {
			alert("delete");
		});
	});
</script>

</head>
<body>
	<input type="button" id="btn" value="增加"></input>
	<div id="parent">
		<div class="child">
			<input type="button" value="删除" class="delete">
		</div>
		<div id="hide_child">
			<div class="child" style="display: none">
				<input type="button" value="删除" class="delete">
			</div>
		</div>
	</div>
</body>
</html>
 可能clone没有把事件监听也clone进去吧。 除了第一个删除有反应,其他的全没有反应啊。why
   发表时间:2011-08-10  
你可以使用live或delegate方法
0 请登录后投票
   发表时间:2011-08-10  
BoneWG 写道
你可以使用live或delegate方法


嗯。写完这个项目了解了解。
0 请登录后投票
   发表时间:2011-08-10  
clone(true)
0 请登录后投票
   发表时间:2011-08-10  
楼上正解,clone(true)表示连事件一起CLONE
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics