jquery 就绪函数:脚本未检测到函数

发布于 2024-09-04 02:07:01 字数 974 浏览 8 评论 0原文

有一个带有占位符(普通 div)的网页。通过 ajax 调用,我将

$(document).ready(function() {

    //    $('#site_preferences_content div').each(function() {
    //        if (typeof (window.tristate_DisableControl) == 'undefined') {

    //            if (typeof (window.console) != 'undefnied')
    //                console.log((new Date()).toTimeString() + ' not logable');

    //            pausecomp(1000);

    //        }
    //        else
    //            tristate_DisableControl(this);
    //    }); //end $('#site_prefrences_content div').each()

    setTimeout(function() {
        $('#site_preferences_content div').each(function() { tristate_DisableControl(this); })
    }, 1000);

});  

我认为当 $(document).ready() 执行时 DOM 将被正确加载......

There is a web page with place holder (a normal div). Via ajax calls I am loading a <form> and a <script> into the place holder. The script contains necessary javascript to initialize the form (i.e. for e.g. disable the controls so as to make form read-only, etc). Here is a piece of code I have; it works, but the commented part doesn't work. Because the script engine cannot find the object tristate_DisableControl which is a function in one of those scripts I call via ajax.

$(document).ready(function() {

    //    $('#site_preferences_content div').each(function() {
    //        if (typeof (window.tristate_DisableControl) == 'undefined') {

    //            if (typeof (window.console) != 'undefnied')
    //                console.log((new Date()).toTimeString() + ' not logable');

    //            pausecomp(1000);

    //        }
    //        else
    //            tristate_DisableControl(this);
    //    }); //end $('#site_prefrences_content div').each()

    setTimeout(function() {
        $('#site_preferences_content div').each(function() { tristate_DisableControl(this); })
    }, 1000);

});  

I thought by the time $(document).ready() executes the DOM will be properly loaded...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-09-11 02:07:02

我不确定我是否正确理解了您的问题,但是如果 AJAX 调用返回 tristate_DisableControl 的声明,那么不,DOMReady 不会等到所有 AJAX调用被执行(毕竟,它怎么可能知道将进行多少次 AJAX 调用?)

您必须使用 AJAX 函数的 success / complete 回调来查找加载完成后退出。

I'm not sure I've understood your question correctly, but if the declaration of tristate_DisableControl is returned by an AJAX call, then no, DOMReady does not wait until all AJAX calls are executed (after all, how could it possibly know how many AJAX calls will be made?)

You have to use the success / complete callback of the AJAX function to find out when it has finished loading.

浮生未歇 2024-09-11 02:07:02

加载整个文档时会触发文档就绪事件,其中不包括使用 ajax 加载的脚本,因为如果不是这种情况,如果您的脚本不断进行 ajax 调用,则就绪事件可能永远不会触发。

为此,您应该在 ajax 加载方法上使用回调。

Document ready event fires when whole document is loaded, which excludes scripts loaded using ajax, because if that wasn't the case, ready event would probably never fire if your script keeps making ajax calls.

You should use callbacks on ajax loading methods for this.

佞臣 2024-09-11 02:07:01

ready 事件在页面加载完成时发生。它不会等待异步 AJAX 调用完成。

要在加载额外内容后运行代码,请使用 load 方法的回调。例子:

$('#site_preferences_content').load('content.html', function() {
  $('#site_preferences_content div').each(function() {
    tristate_DisableControl(this);
  }
});

The ready event happens when the page is finished loading. It doesn't wait for asynchronous AJAX calls to complete.

To run the code once the extra content is loaded, you use the callback of the load method. Example:

$('#site_preferences_content').load('content.html', function() {
  $('#site_preferences_content div').each(function() {
    tristate_DisableControl(this);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文