87,907
社区成员
发帖
与我相关
我的任务
分享
//这样写的话,当页面加载结束后,给当前页面中id='d'的标签绑定click事件,不会给通过js追加的元素绑定click事件,建议LZ看下jQuery事件绑定 bind() live() trigger()区别
$(document).ready(function(){
$("#d").click(function(){
$(this).css("background-color","yellow");
});
$("#c").click(function(){
$(this).after(" <button type='button' id='d'>add</button>");
});
});
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#c").click(function(){
$(this).after(" <button type='button' id='d'>add</button>");
$("#d").click(function(){
$(this).css("background-color","yellow");
});
});
});
</script>
</head>
<body>
<p id="c">aaaa</p>
</html>
</body>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#d").live("click",function(){
$(this).css("background-color","yellow");
});
$("#c").click(function(){
$(this).after(" <button type='button' id='d'>add</button>");
});
});
</script>
</head>
<body>
<p id="c">aaaa</p>
</html>
</body>