myW*_*SON 5 html javascript jquery dom
我生成了大量的HTML $(document).ready().我有一个简单的窗口系统.但不仅是它生成$(document).ready()- 还有一些HTML元素(不同的JS文件放入内容$(document).ready()).我希望在$(document).ready()调用之后生成我的窗口系统.那么在$(document).ready()完成注册的所有代码后如何处理要调用的函数?
Dmi*_*pov 10
$(window).load(function(){
//some code after ready
});
Run Code Online (Sandbox Code Playgroud)
还有另一个事件会被解雇.它是$(窗口).load(); 加载所有资源后会触发此操作.
但也许你想要这个:
function loadWindowSystem(){
// load window system here
}
$(document).ready(function(){
// do some html stuff here
loadWindowSystem();
})
Run Code Online (Sandbox Code Playgroud)
这样您就可以在函数中分离代码.
我通常不提倡使用setTimeout,但您可以在@jfriend00 的回答基础上创建更抽象的方法:
$(document).ready(function() {
setTimeout(function() {
$(document).trigger('afterready');
}, 1);
});
$(document).bind('afterready', function() {
// call your code here that you want to run after all $(document).ready() calls have run
});
Run Code Online (Sandbox Code Playgroud)
如果您希望在所有调用之后立即触发某些内容$(document).ready(),您可以将其放置在页面中的任何位置:
$(document).ready(function() {
setTimeout(function() {
// call your code here that you want to run after all $(document).ready() calls have run
}, 1);
});
Run Code Online (Sandbox Code Playgroud)
这将与所有其他 document.ready 调用一起调用,但它设置一个较短的超时,该超时将在所有其他 document.ready 调用完成后执行。