如何使用 jQuery 默认预加载所有选项卡
如果我有 4 个选项卡,其中前 2 个使用 ajax 加载,后 2 个是静态的,那么默认情况下如何预加载 2 个 ajax 选项卡?
目前,只有第一个选项卡会自动加载,第二个选项卡会在单击时加载。我希望它们都被加载,这样当我单击第二个时,内容就已经加载了。我尝试像这样调用第二个选项卡上的加载事件:
$(document).ready(function () {
$("#main-tabs").tabs({
cache: true
});
$("#main-tabs").tabs("load", 1);
});
这会加载第二个选项卡,但由于某种奇怪的原因,第一个选项卡根本不加载;即使我单击不同的选项卡并返回第一个选项卡,它也不会加载。
然后我尝试了这样的事情:
$(document).ready(function () {
$("#main-tabs").tabs({
cache: true
});
$("#main-tabs").tabs("load", 0);
$("#main-tabs").tabs("load", 1);
});
相同的结果,第二个选项卡已加载,第一个选项卡未加载。
如何自动加载所有这些(ajax 的)?
If I have 4 tabs where the first 2 are loaded with ajax and the last 2 are static, how do I pre-load the 2 ajax tabs by default?
For the moment, only the first tab is automatically loaded, the second one is loaded when clicked. I want them both to be loaded so that when I click the second one, the content is already loaded. I tried to call the load event on the second tab like this:
$(document).ready(function () {
$("#main-tabs").tabs({
cache: true
});
$("#main-tabs").tabs("load", 1);
});
And this loads the second tab, but for some strange reason, the first one doesn't load at all; not even when I click a different tab and click back on the first one, it will not load.
Then I tried something like this:
$(document).ready(function () {
$("#main-tabs").tabs({
cache: true
});
$("#main-tabs").tabs("load", 0);
$("#main-tabs").tabs("load", 1);
});
Same result, second tab is loaded, first one is not.
How can all of them (ajax ones) be loaded automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题的根源
两个事实:
load
方法加载选项卡时,如果另一个 AJAX 加载请求正在进行中,则该请求将被中止(可能是因为如果您选择一个选项卡,它通过 AJAX 加载,然后快速选择另一个要加载的选项卡,jQuery 假设您不想加载两个选项卡 - 只是最后一个选项卡)。.tabs("load",0)
来填充第一个选项卡。结合这两个事实,您就会明白发生了什么。首先调用
load
以填充第一个选项卡,然后再次调用以填充第二个选项卡。第二个负载抵消了第一个负载。可能的解决方案
由于您无法同时在同一选项卡菜单上发出多个
load
,因此我们必须找到另一种方法。该解决方案使用选项卡的load
选项按顺序加载每个选项卡。换句话说,当一个加载完成后,它开始加载下一个。当该任务完成后,它会加载下一个任务,依此类推。此方法可能的改进:
我如何找出问题所在?
通过使用萤火虫。它是 Firefox 的插件。 Firebug 在其控制台中显示所有 AJAX 请求,如屏幕截图所示,不难弄清楚发生了什么:) 我真的推荐它。 (大多数主流浏览器都有类似的工具 - 在 chrome 中按 ctrl+shift+j 或在 IE 中按 F12。)
Source of the problem
Two facts:
load
method, if another AJAX load request is in progress, this will be aborted (probably because if you select one tab and it loads by AJAX and then quickly select another to be loaded, jQuery assumes you don't want to load both - just the last one)..tabs("load",0)
will be called by default to populate the first tab.Combining these two facts, you see what's going on.
load
is being called first to populate the first tab, and then called again to populate the second one. The second load cancels out the first.Possible solution
Since you can't issue multiple
load
s on the same tab menu at the same time, we'll have to find another way. This solution uses theload
option of tabs to load each tab in sequence. In other words, when one load is finished, it starts loading the next one. When that one is finished, it loads the next one and so on.Possible improvements to this method:
How did I find out what was wrong?
By using firebug. It's an addon for firefox. Firebug shows all AJAX requests in its console, and as the screenshot shows, it wasn't that hard to figure out what was going on :) I really recommend it. (There are similar tools for most major browsers - press ctrl+shift+j in chrome or F12 i IE.)