本文介绍了在$(document).ready()之前运行一个函数触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将多个文件中的多个函数附加到$(document).ready中,并希望将单个函数附加在它们之前,作为$(document).ready处理的第一个或在$ (文档).ready处理程序。



有没有办法处理jQuery在内部触发的函数的顺序,作为jQuery.fn.ready的一部分或钩住函数在jQuery.fn.ready之前调用。



在第三方脚本中编辑jQuery.fn.ready可以安全地做,否则会导致其他的可怕的敲击效果第三方插件(除了自己编辑jQuery.fn.ready的插件外)





作为示例

  $(document).ready(function b(){}); 
....
$(document).ready(function a(){/ *可选设置* /});
....
$(document).ready(function c(){});

函数需要首先发生与订单无关,但只能在几页上调用。我不能保证b或c的顺序,所以我不能在b或c的开始触发自定义事件。



所以我想要一个通用的解决方案,我可以使用它强制放置在jQuery的内部readyList或钩子到jQuery的就绪功能,并安全地编辑它,所以调用在readyList中的任何其他功能之前。



[进一步编辑]



如果可能,我宁愿不必须重新设计javascript代码执行顺序,或者必须触摸除功能a()之外的任何其他代码。我知道重组将允许我解决这个问题,但我宁愿只写一个单一的功能,如

  $。 beforeReady(function a(){}); 


解决方案

这将是一个触发自定义所有您的其他文件将 bind 的事件,您将只有一个准备好的处理程序,然后触发自定义事件。



所以,代码中的其他地方,而不是使用 $(document).ready ,您将使用

  $(window).bind('setup',function(){
//代码在这里...
});

和/或

  $(window).bind('loaded',function(){
// some other code here ...
});

在您唯一的准备中处理程序你可以这样做:

  $(document).ready(function(){
$窗口).trigger('setup');
$(window).trigger('loaded'):
});


I've attached multiple functions in multiple files to $(document).ready and would like to attach a single function to happen before them as either the first that $(document).ready handles or to independently trigger before the $(document).ready handler.

Is there any way to handle the order of functions that jQuery triggers internally as part of jQuery.fn.ready or to hook in a function that calls just before jQuery.fn.ready.

Is editing jQuery.fn.ready in a 3rd party script safe to do or will it cause horrible knock on effects in other 3rd party plugins (apart from plugins that edit jQuery.fn.ready themselves)

[Edit]:

as an example

$(document).ready(function b() {});
....
$(document).ready(function a() {/* optional setup*/});
....
$(document).ready(function c() {});

function a needs to happen first irrelevant of order, but only gets called on a few pages. I cant guarantee the order of b or c so I cant trigger custom events at the start of b or c to trigger a.

So I would like a generic solution that I can use to forcefully place a at the start of jQuery's internal readyList or hook into jQuery's ready function and edit it safely so it calls a before any of the other functions in readyList.

[Further Edit]

If possible I would rather not have to redesign the javascript code execution order or have to touch any other code apart from function a(). I'm aware restructuring would allow me to get around this problem but I'd rather just write a single function like

$.beforeReady(function a() {});
解决方案

This would be a time where I would trigger a custom event that all of your other files would bind to, you would only have one ready handler, which would do stuff, then trigger the custom event.

So, somewhere else in your code, instead of using $(document).ready, you would use

$(window).bind('setup', function() {
   //code here...
});

And/or

$(window).bind('loaded', function() {
   //some other code here...
});

And in your one and only ready handler, you'd do something like this:

$(document).ready(function() {
   $(window).trigger('setup');
   $(window).trigger('loaded'):
});

这篇关于在$(document).ready()之前运行一个函数触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:37