求助 关于jquery的$(selector).after(content)

maliemin123 2011-12-31 10:15:53


<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$("#d").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>



为什么点了add按钮后颜色不变???
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenlin1019 2011-12-31
  • 打赏
  • 举报
回复

//这样写的话,当页面加载结束后,给当前页面中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>");
});
});
p2227 2011-12-31
  • 打赏
  • 举报
回复
<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>
kfreewind 2011-12-31
  • 打赏
  • 举报
回复
好牛B的样子

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧