代码如下:
$(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中调用什么类型的函数并不重要,无论它是什么,它都会多次触发。
发布于 2009-12-18 04:16:26
这是因为您每次加载ajaxed内容时都绑定了livequery函数…
你不需要这样做,这就是使用livequery的好处……
移动这一小段代码:
$("#content .main .new").livequery( function(){
Boxy.alert("Sample Alert", null, {title: 'Alert!'});
});离开getpage1()并进入document.ready块,如下所示:
$(document).ready(function(){
$(".menu ul li a").click( function(){
curpage = $(this).attr('href');
loadpage(curpage);
});
$("#content .main .new").livequery( function(){
Boxy.alert("Sample Alert", null, {title: 'Alert!'});
});
});发布于 2009-12-18 04:28:15
看起来我只需要让监听程序失效。现在起作用了。变化是这样的:
$("#content .main .new").livequery( function(){
Boxy.alert("Sample Alert", null, {title: 'Alert!'});
$("#content .main .new").expire();
});https://stackoverflow.com/questions/1924179
复制相似问题