load()顺序,而不是同时使用jQuery [英] load() sequentially, instead of at the same time with jQuery

查看:82
本文介绍了load()顺序,而不是同时使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,我想从导航栏获取所有链接,并将链接页面中的div加载到大容器中。到目前为止我已经有以下几个了:

  $('nav a')。each(function(index){
var to_load = $(this).attr('href')+'#slides section';
$('< section>')load(to_load,function(){
$ ('#slides')。append($(this).html());
});
});

除了事实上,它几乎总是不正常,可能是由于加载它异步地。有没有一种方法可以一次加载,所以这些部分会按顺序进行,同时仍然保持灵活性?



提前感谢!

解决方案

我会尝试使用 async:false 提到 jQuery.ajax文档
不知道它是否与加载一起使用,但我敢打赌。



另外因为性能原因,不要使用:

  $(this).attr('href')+'#slides部分'

 code> this.href +'#slides section'

为避免转换这个到一个jQuery元素(你只需要它来获取 href 属性)。


I'm developing a site where I want to get all the links from a navigation bar, and load a div from the linked pages into a big container. What I have so far is the following:

$('nav a').each(function(index){
    var to_load = $(this).attr('href') + '#slides section';
    $('<section>').load(to_load, function(){
        $('#slides').append($(this).html());
    });
});

This works great, apart from the fact that it is almost alway out of order, probably due to loading it asynchronously. Is there a way to load each at a time, so the sections will be in order, while still keeping it flexible?

Thanks heaps in advance!

解决方案

I'd try with async: false mentioned jQuery.ajax documentation. Don't know if it works with load but I'd bet it does.

Also, for performance reason, don't use:

$(this).attr('href') + '#slides section'

but

this.href + '#slides section'

To avoid converting this into a jQuery element (you only need it to get the href attribute).

这篇关于load()顺序,而不是同时使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆