所有其他JS执行后运行jQuery [英] Running jQuery after all other JS has executed

查看:96
本文介绍了所有其他JS执行后运行jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去,我已经使用$(window).onload在其他脚本加载后运行一个代码块。在这种情况下,我无法更改JS文件的加载顺序,并且我关心的代码需要操作由另一个进一步加载到页面的JS文件动态插入的DOM节点。这两个脚本位于文档底部附近。有没有人对这种情况有任何建议?

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?

推荐答案

如果你知道要预期的元素,你可以使用 setInterval 轮询DOM以查看它们何时被插入。例如,这是您可以轮询ID foo 的元素:

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:

$(window).load(function ()
{
    var i = setInterval(function ()
    {
        if ($('#foo').length)
        {
            clearInterval(i);
            // safe to execute your code here
        }
    }, 100);
});

这篇关于所有其他JS执行后运行jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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