BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #18489同步于 2011/5/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

jquery求助

hellodao
2011/5/23镜像同步7 回复
代码如下: <html> <head> <script type="text/javascript" src="jquery-1.6.1.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").click(function(){ $(this).hide(); }); var seq = 1; $("button#0").click(function(){ var button = $("<button type='button' id="+seq+">btn add "+seq+"</button>"); seq += 1; var parent = $("body"); parent.append(button); }); $("button#3").click(function(){ alert("hello"); }); }); </script> </head> <body> <p>If you click on me, I will disappear.</p><br/> <button type="button" id="0">test</button> </body> </html> 说明: 原始有个test按钮,每点击一次就会增加一个button,然后点击id为3的button,应该弹出“hello”,但是实际上没有。 这是为什么呢
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
Adun机器人#1 · 2011/5/23
$("button#3").click(function(){ alert("hello"); }); 这段是在页面加载完成后就执行了.这时候页面上还没有id为3的按钮,所以这个绑定没生效. 略改动了一下,在jquery1.4.2下测试通过: <html> <head> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").click(function(){ $(this).hide(); }); var seq = 1; $("button#0").click(function(){ var button = $("<button type='button' id="+seq+">btn add "+seq+"</button>"); if (button.attr("id") == 3){ button.click(function(){ alert("hello"); }); } seq += 1; var parent = $("body"); parent.append(button); }); }); </script> </head> <body> <p>If you click on me, I will disappear.</p><br/> <button type="button" id="0">test</button> </body> </html>
xw2423机器人#2 · 2011/5/23
【 在 hellodao (hellodao) 的大作中提到: 】 : 标 题: jquery求助 : 发信站: 北邮人论坛 (Mon May 23 11:18:37 2011), 站内 : : 代码如下: : <html> : <head> : <script type="text/javascript" src="jquery-1.6.1.js"></script> : <script type="text/javascript"> : $(document).ready(function(){ : $("p").click(function(){ : $(this).hide(); : }); : var seq = 1; : $("button#0").click(function(){ : var button = $("<button type='button' id="+seq+">btn add "+seq+"</button>"); : seq += 1; : var parent = $("body"); : parent.append(button); : }); : : $("button#3").click(function(){ $("button#3").live('click', function(){ : alert("hello"); : }); : }); : </script> : </head> : : <body> : <p>If you click on me, I will disappear.</p><br/> : <button type="button" id="0">test</button> : </body> : : </html> : : 说明: : 原始有个test按钮,每点击一次就会增加一个button,然后点击id为3的button,应该弹出“hello”,但是实际上没有。 : : 这是为什么呢 : -- : : ※ 来源:·北邮人论坛 http://bbs.byr.cn·[FROM: 124.16.138.*]
hellodao机器人#3 · 2011/5/23
多谢楼上两位,两种不同的思路
Adun机器人#4 · 2011/5/23
【 在 xw2423 的大作中提到: 】 : : 【 在 hellodao (hellodao) 的大作中提到: 】 : : 标 题: jquery求助 : ................... .live~~~还有这种东东哈~~~学习了~~~
ilee机器人#5 · 2011/5/23
唔 动态绑定的问题。 三种办法; clone(true) live() delegate()
polarwyp机器人#6 · 2011/5/23
补充一下,$("button#3")这种写法一定要戒掉,直接使用id选择器即可
yqiao2007机器人#7 · 2011/5/24
绑定太早了呗,创建id=3的时候进行绑定啊