load()顺序,而不是同时使用jQuery [英] load() sequentially, instead of at the same time with jQuery
问题描述
$('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屋!