livequery 多次触发
代码如下:
$(document).ready(function(){
$(".menu ul li a").click( function(){
curpage = $(this).attr('href');
loadpage(curpage);
});
});
function loadpage(){
switch (curpage){
case "#page1":
getpage1();
break;
case "#page2":
getpage2();
break;
}
}
function getpage1() {
$("#content .main").load("/page1.htm");
$("#content .main .new").livequery( function(){
Boxy.alert("Sample Alert", null, {title: 'Alert!'});
});
}
function getpage2() {
$("#content .main").load("page2.htm");
}
因此,如果我单击 #page1 的链接,事情就会按预期进行。如果我单击 #page2 的链接,一切都会按预期进行。但是当我第二次单击 #page1 的链接时,livequery 函数将触发两次。我可以单击#page2 的链接,然后第三次单击#page1 的链接,livequery 函数将触发三次。
我对此一无所知。请帮忙。无论我在 livequery 中调用哪种函数,无论它是什么,它都会触发多次。
Here's the code:
$(document).ready(function(){
$(".menu ul li a").click( function(){
curpage = $(this).attr('href');
loadpage(curpage);
});
});
function loadpage(){
switch (curpage){
case "#page1":
getpage1();
break;
case "#page2":
getpage2();
break;
}
}
function getpage1() {
$("#content .main").load("/page1.htm");
$("#content .main .new").livequery( function(){
Boxy.alert("Sample Alert", null, {title: 'Alert!'});
});
}
function getpage2() {
$("#content .main").load("page2.htm");
}
So, if I click the link for #page1, things work as expected. If I click the link for #page2, things work as expected. But when I click the link for #page1 for the second time, the livequery function will fire twice. I can click the link for #page2, then click the link for #page1 for the third time and the livequery function will fire three times.
I'm at a loss for words on this one. Please help. It doesn't matter what kind of function I call within livequery, whatever it is, it will fire multiple times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为每次加载 ajax 内容时都会绑定 livequery 函数...
您不必这样做,这就是使用 livequery 的优点...
将这段代码移出
getpage1 ()
并进入document.ready
块,如下所示:That's because you are binding the livequery function every time you load that bit of ajaxed content...
You do not have to do that, that's the advantage of using livequery...
Move this bit of code:
out of the
getpage1()
and into thedocument.ready
block like so:看起来我只需要让监听器过期。现在可以工作了。这是变化:
Looks like I just needed to expire the listener. Works now. Here's the change: