jQuery多文档就绪队列顺序
我知道jQuery中对$(function(){})的调用是按照定义的顺序执行的,但是我想知道您是否可以控制队列的顺序 例如,是否可以在“Hello World 1”之前调用“Hello World 2”:jQuery多文档就绪队列顺序,jquery,domready,document-ready,Jquery,Domready,Document Ready,我知道jQuery中对$(function(){})的调用是按照定义的顺序执行的,但是我想知道您是否可以控制队列的顺序 例如,是否可以在“Hello World 1”之前调用“Hello World 2”: 问题是这是否可能。。。我已经知道这违背了最佳实践;) 这些函数被添加到一个私有数组readyList,因此我认为它不可用于操作 这是可以做到的,但并不容易。您可能必须破解jQuery本身。在jQuery开始在while循环中调用这些函数之前,您必须添加代码来检查readyList数组,并根据
问题是这是否可能。。。我已经知道这违背了最佳实践;) 这些函数被添加到一个私有数组
readyList
,因此我认为它不可用于操作
这是可以做到的,但并不容易。您可能必须破解jQuery本身。在jQuery开始在
while
循环中调用这些函数之前,您必须添加代码来检查readyList
数组,并根据您的偏好对元素重新排序。下面是如何执行此操作的:
// lower priority value means function should be called first
var method_queue = new Array();
method_queue.push({
method : function()
{
alert('Hello World 1');
},
priority : 2
});
method_queue.push({
method : function()
{
alert('Hello World 2');
},
priority : 1
});
function sort_queue(a, b)
{
if( a.priority < b.priority ) return -1;
else if( a.priority == b.priority ) return 0;
else return 1;
}
function execute_queue()
{
method_queue.sort( sort_queue );
for( var i in method_queue ) method_queue[i].call( null );
}
// now all you have to do is
execute_queue();
//较低的优先级值意味着应该首先调用函数
var method_queue=新数组();
方法\u queue.push({
方法:函数()
{
警报(“你好,世界1”);
},
优先事项:2
});
方法\u queue.push({
方法:函数()
{
警报(“你好,世界2”);
},
优先事项:1
});
函数排序队列(a、b)
{
if(a.priority
您可以阅读更多关于它的信息您可以使用jQuery promise实现类似的功能 以下是jQuery.ready.promise帮助管理DOM就绪块的执行顺序的示例:
jQuery(function () {
var testDivHeight = jQuery("#test-div").outerHeight();
if(testDivHeight) {
alert("Height of test div is: "+testDivHeight);
} else {
alert("Sorry I cannot get the height of test div!");
}
});
jQuery(function () {
jQuery('body').append('<div style="background: #C00; height: 100px;" id="test-div"></div>');
});
jQuery(函数(){
var testDivHeight=jQuery(“#test div”).outerHeight();
if(testDivHeight){
警报(“测试分区的高度为:”+testDivHeight);
}否则{
警报(“对不起,我无法获取测试div的高度!”);
}
});
jQuery(函数(){
jQuery('body')。append('');
});
小提琴:jQuery(function () {
jQuery.ready.promise().done(function () {
var testDivHeight = jQuery("#test-div").outerHeight();
if(testDivHeight) {
alert("Height of test div is: "+testDivHeight);
} else {
alert("Sorry I cannot get the height of test div!");
}
});
});
jQuery(function () {
jQuery('body').append('<div style="background: #C00; height: 100px;" id="test-div"></div>');
});
jQuery(函数(){
jQuery.ready.promise().done(函数(){
var testDivHeight=jQuery(“#test div”).outerHeight();
if(testDivHeight){
警报(“测试分区的高度为:”+testDivHeight);
}否则{
警报(“对不起,我无法获取测试div的高度!”);
}
});
});
jQuery(函数(){
jQuery('body')。append('');
});
小提琴:为什么要定义2个不同的$(function(){}块?我只需要使用1,然后按照执行顺序把东西放在最上面。@格雷格:考虑使用站点上的第三方工具使用jQuery和DoCuff.Read的情况。进一步考虑,您可能希望将代码添加到依赖于第三方文档的页面。它很容易控制或预测操作顺序。
jQuery(function () {
jQuery.ready.promise().done(function () {
var testDivHeight = jQuery("#test-div").outerHeight();
if(testDivHeight) {
alert("Height of test div is: "+testDivHeight);
} else {
alert("Sorry I cannot get the height of test div!");
}
});
});
jQuery(function () {
jQuery('body').append('<div style="background: #C00; height: 100px;" id="test-div"></div>');
});