返回信息流代码如下:
<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”,但是实际上没有。
这是为什么呢
这是一条镜像帖。来源:北邮人论坛 / java / #18489同步于 2011/5/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
jquery求助
hellodao
2011/5/23镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
$("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>
【 在 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.*]
【 在 xw2423 的大作中提到: 】
:
: 【 在 hellodao (hellodao) 的大作中提到: 】
: : 标 题: jquery求助
: ...................
.live~~~还有这种东东哈~~~学习了~~~