JQuery Prototype 通过 Ajax 重新加载冒泡
我有一个按钮,单击时显示“HI”。该按钮位于名为“Panel1”的 div 中,我使用 AJAX 对其进行更新。第一次点击后,当Panel1重新加载时,按钮的点击甚至不再触发。我想这与事件冒泡有关...但我在过去三天似乎无法解决它。示例代码如下:-
<script language="JavaScript" type="text/javascript">
document.observe("dom:loaded", function() {
$$("#ShowDialog").invoke('observe', 'click', function() {
alert("HI");
AjaxPanel.reload($("Panel1"));
});
</script>
</head>
<body>
<div id="Panel1">
.
.
.
<input name="btnAdd" type="submit" value="Add" id="ShowDialog">
</div>
</body>
</html>
非常感谢任何建议。
I've got a button that displays "HI" when clicked. The button is in a div called "Panel1", which I'm using AJAX to update. After clicking for the first time, when Panel1 is reloaded, the button's click even does not fire anymore. I guess this has to do with event bubbling... but I just can't seem to solve it for the last 3 days. Sample code is as follows:-
<script language="JavaScript" type="text/javascript">
document.observe("dom:loaded", function() {
$("#ShowDialog").invoke('observe', 'click', function() {
alert("HI");
AjaxPanel.reload($("Panel1"));
});
</script>
</head>
<body>
<div id="Panel1">
.
.
.
<input name="btnAdd" type="submit" value="Add" id="ShowDialog">
</div>
</body>
</html>
Any advice is deeply appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将替换
Panel1
的全部内容,包括事件侦听器附加到的按钮。因此,当窗口首次加载时,事件侦听器已正确附加。但是当ajax请求替换div的内容时,按钮也会消失。即使新内容具有类似的按钮,它(新按钮)也不会附加事件侦听器。
我不知道您正在使用的 AjaxPanel“类”,但这是我使用直接原型的方法。
或者,您可以将按钮放在面板之外,这样它就不会被替换。或者您可以使用 jQuery 和
.live()
方法。You're replacing the entire contents of
Panel1
, including the button that the event listener is attached to.So when the window first loads, the event listener is correctly attached. But when the ajax request replaces the content of the div, the button disappears too. Even if the new content has a similar button, it (the new button) won't have an event listener attached.
I don't know the AjaxPanel "class" you're using, but here's how I'd do it with straight Prototype
Alternatively, you can just put the button outside the panel, so it doesn't get replaced. Or you can use jQuery, and the
.live()
method.