jQuery 选项卡 - 为什么请求成倍增加?
假设我有一个 jQuery UI 选项卡(已加载 ajax),定义为调用页面 abc.html。在该页面上,我调用另一个页面,比方说 def.html。在我的 jQuery 选项卡定义中,我定义了选项卡内的链接应在选项卡内打开,这就是定义:
$(document).ready(function() {
$("#tabs").tabs({
select: function(event, ui) {
$(ui.panel).undelegate('a', 'click');
},
load: function(event, ui) {
$(ui.panel).delegate('a', 'click', function() {
$(ui.panel).load(this.href);
return false;
});
}
});
});
好的,所以在选项卡中的链接之间来回切换时,这可以正常工作。但是,在我的页面 def.html 上,我有一个按钮,我想用它来加载初始选项卡。我就是这样做的:
$("#cancelButton").click(function(event){
event.preventDefault();
$("#tabs").tabs('load', $("#tabs").tabs('option', 'selected'));
});
这调用我最初的 abc.html 就可以了。问题是,当我从新加载的 abc.html 再次调用 def.html 的链接时,对 def.html 的请求发生了两次。我再次单击我的cancelButton,它重定向到abc.html,我再次单击def.html,现在我的请求被调用三次,依此类推。
我猜测某个地方的某些东西被多次绑定,但不会被解除绑定。任何帮助都可以,谢谢。
Let's say I have a jQuery UI tab (ajax loaded), defined to call page abc.html. On that page I call another page, let's say def.html. In my jQuery tab definition I have defined that links within tabs should be opened within tabs, this is the definition:
$(document).ready(function() {
$("#tabs").tabs({
select: function(event, ui) {
$(ui.panel).undelegate('a', 'click');
},
load: function(event, ui) {
$(ui.panel).delegate('a', 'click', function() {
$(ui.panel).load(this.href);
return false;
});
}
});
});
Ok so this works fine when going back and forth between links in a tab. However, on my page def.html I have a button that I want to use to load the initial tab. This is how I do it:
$("#cancelButton").click(function(event){
event.preventDefault();
$("#tabs").tabs('load', $("#tabs").tabs('option', 'selected'));
});
This calls my initial abc.html just fine. Problem is, when I call the link to def.html again from my newly loaded abc.html, request for def.html happens twice. I click on my cancelButton again, it redirects to abc.html, I click on def.html again and now my request is called three times and so on.
I'm guessing something somewhere gets bound multiple times, but does not get unbound. Any help would do, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想通了。发生的事情是当我调用时,
只有 tabsload 事件才会触发。这意味着 tabsselect 事件之前没有被触发,因此链接没有被取消委托并且被绑定了多次。
解决这个问题的方法是取消委派,然后在加载时委派选项卡内容中的链接,删除任何选项卡选择功能。这是我的新选项卡定义:
Ok so I figured it out. What happened was when I called
only tabsload event would fire. This means that tabsselect event was not fired before, hence links weren't getting undelegated and were bound multiple times.
What solved it was doing both undelegating and then delegating the links in tab content on load, removing any tabsselect functionality. Here's my new tab definition:
我认为这是因为您在原始选择器
$("#tabs")
上调用方法,而不是在初始调用tabs
的结果上调用方法。尝试保存引用并在取消按钮处理程序中使用它:I think it is because you are calling methods on the original selector
$("#tabs")
instead of on the result of the initial call totabs
. Try saving the reference and using it in your cancel button handler: