jquery 就绪函数:脚本未检测到函数
有一个带有占位符(普通 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否正确理解了您的问题,但是如果 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.加载整个文档时会触发文档就绪事件,其中不包括使用 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.
ready
事件在页面加载完成时发生。它不会等待异步 AJAX 调用完成。要在加载额外内容后运行代码,请使用
load
方法的回调。例子: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: