且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

页面加载完成后执行函数

更新时间:2022-04-01 06:48:20

这可能对你有用:

document.addEventListener('DOMContentLoaded', function() {
   // your code here
}, false);

或如果您对 jquery 感到满意,

or if your comfort with jquery,

$(document).ready(function(){
// your code
});

$(document).ready() 在 DOMContentLoaded 上触发,但该事件在浏览器之间并没有一致触发.这就是为什么 jQuery 很可能会实施一些繁重的解决方法来支持所有浏览器.这将使使用普通 Javascript 来精确"模拟行为变得非常困难(但当然并非不可能).

$(document).ready() fires on DOMContentLoaded, but this event is not being fired consistently among browsers. This is why jQuery will most probably implement some heavy workarounds to support all the browsers. And this will make it very difficult to "exactly" simulate the behavior using plain Javascript (but not impossible of course).

正如 Jeffrey Sweeney 和 J Torres 所建议的,我认为在触发如下函数之前***有一个 setTimeout 函数:

as Jeffrey Sweeney and J Torres suggested, i think its better to have a setTimeout function, before firing the function like below :

setTimeout(function(){
 //your code here
}, 3000);