如何使用 jQuery 默认预加载所有选项卡

发布于 2024-09-19 02:38:13 字数 666 浏览 12 评论 0原文

如果我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-09-26 02:38:14

问题的根源

两个事实:

  • 当 jQuery 使用其 load 方法加载选项卡时,如果另一个 AJAX 加载请求正在进行中,则该请求将被中止(可能是因为如果您选择一个选项卡,它通过 AJAX 加载,然后快速选择另一个要加载的选项卡,jQuery 假设您不想加载两个选项卡 - 只是最后一个选项卡)。
  • 当您设置由 AJAX 加载第一个选项卡时,默认情况下将调用 .tabs("load",0) 来填充第一个选项卡。

结合这两个事实,您就会明白发生了什么。首先调用 load 以填充第一个选项卡,然后再次调用以填充第二个选项卡。第二个负载抵消了第一个负载。

可能的解决方案

由于您无法同时在同一选项卡菜单上发出多个load,因此我们必须找到另一种方法。该解决方案使用选项卡的 load 选项按顺序加载每个选项卡。换句话说,当一个加载完成后,它开始加载下一个。当该任务完成后,它会加载下一个任务,依此类推。

//the indexes to be loaded.
//In your case it's only index 1 (index 0 is loaded automatically)
var indexesToLoad = [1];

var loadNextTab = function() {
    if (indexesToLoad.length == 0) return;
    var index = indexesToLoad.shift();
    $("#main-tabs").tabs("load",index);
};

$("#main-tabs").tabs({
    cache: true,
    load: loadNextTab
});

此方法可能的改进:

  • 不要指定要加载的索引,而是让它自己找出哪些选项卡是 AJAX 选项卡并加载它们
  • 使 loadNextTab 可以同时在多个选项卡式菜单上使用
  • 使其可与其他加载回调函数集成(如果这是一个词)

我如何找出问题所在?

通过使用萤火虫。它是 Firefox 的插件。 Firebug 在其控制台中显示所有 AJAX 请求,如屏幕截图所示,不难弄清楚发生了什么:) 我真的推荐它。 (大多数主流浏览器都有类似的工具 - 在 chrome 中按 ctrl+shift+j 或在 IE 中按 F12。)

Firebug 控制台的屏幕截图显示 ajax 请求

Source of the problem

Two facts:

  • When jQuery loads a tab with its 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).
  • When you set the first tab to be loaded by AJAX, .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 loads on the same tab menu at the same time, we'll have to find another way. This solution uses the load 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.

//the indexes to be loaded.
//In your case it's only index 1 (index 0 is loaded automatically)
var indexesToLoad = [1];

var loadNextTab = function() {
    if (indexesToLoad.length == 0) return;
    var index = indexesToLoad.shift();
    $("#main-tabs").tabs("load",index);
};

$("#main-tabs").tabs({
    cache: true,
    load: loadNextTab
});

Possible improvements to this method:

  • instead of specifying indexes to load, make it figure out itself what tabs are AJAX tabs and load them
  • make it so loadNextTab can be used on multiple tabbed menus at the same time
  • make it integratable (if that's a word) with other load callback functions

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.)

Screenshot of Firebug console showing ajax requests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文