Javascript 在所有其他 JS 执行后运行 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5494773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running jQuery after all other JS has executed
提问by jerome
In the past, I have used $(window).onload to run a block of code after other scripts have loaded. In this case I don't have the ability to change the loading order of the JS files and the code that I am concerned with needs to manipulate DOM nodes that are dynamically inserted by another JS file that is loaded further down the page. Both scripts are located near the bottom of the document. Does anyone have any suggestions for this scenario?
过去,我使用 $(window).onload 在其他脚本加载后运行代码块。在这种情况下,我无法更改 JS 文件的加载顺序,我所关心的代码需要操作由另一个加载到页面下方的 JS 文件动态插入的 DOM 节点。两个脚本都位于文档底部附近。有没有人对这种情况有任何建议?
回答by Matt Ball
If you know what elements to expect, you can use setInterval
to poll the DOM to see when they've been inserted. For example, this is how you could poll for an element with ID foo
:
如果您知道预期的元素,您可以使用setInterval
轮询 DOM 以查看它们何时被插入。例如,您可以通过以下方式轮询 ID 元素foo
:
$(window).load(function ()
{
var i = setInterval(function ()
{
if ($('#foo').length)
{
clearInterval(i);
// safe to execute your code here
}
}, 100);
});
回答by Robert Hyatt
Even though you cannot change the load order, can you change how it is loaded?
即使你不能改变加载顺序,你能改变它的加载方式吗?
One thing I have used in the past when I wanted to load some javascript and execute some code after I was sure that it had finished loading is jquery's $.getScript:
过去,当我想加载一些 javascript 并在确定加载完成后执行一些代码时,我使用的一件事是 jquery 的 $.getScript:
$.getScript('javascripttoload.js', function() {
//do the DOM manipulation you were talking about...
});
回答by Daniel
If you need to attach an event handler to future DOM nodes, you could use jQuery .live(), otherwise, you can use livequeryplugin.
如果您需要将事件处理程序附加到未来的 DOM 节点,您可以使用 jQuery .live(),否则,您可以使用livequery插件。
example:
例子:
$(".nodes").livequery(function () {
// do something with the nodes
}
which will call the supplied anonymous function for all current and future nodes with the class .nodes
它将为该类的所有当前和未来节点调用提供的匿名函数 .nodes
回答by jerome
The problem was trying to declare a variable to store the jQuery object before the object was on the page. Moving those declarations inside of a code block that was only executed after a click event which was only bound after $(window).load solved the problem.
问题是试图在对象出现在页面上之前声明一个变量来存储 jQuery 对象。将这些声明移动到仅在 $(window).load 之后绑定的单击事件之后才执行的代码块中解决了问题。
Thanks all for the tips, though!
谢谢大家的提示,不过!
回答by SLaks
If the other script executes entirely within a load event, you can add a handler to the same event and put your code in a setTimeout
call (with a delay of 1
), which will force your code to execute during the next break.
如果另一个脚本完全在加载事件中执行,您可以向同一事件添加一个处理程序并将您的代码放入setTimeout
调用中(延迟为1
),这将强制您的代码在下一次中断期间执行。