如何模仿跨浏览器$(document).ready()行为没有JQuery [英] How to imitate cross-browser $(document).ready() behavior without JQuery

查看:134
本文介绍了如何模仿跨浏览器$(document).ready()行为没有JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

$(document).ready等于没有jQuery

有一个我需要写的脚本,需要在DOM上执行,但是我不能依赖JQuery作为脚本的一部分。

I have a script I need to write that needs to execute on DOM ready, but I can't have dependency on JQuery as part of the script.

我想模仿 $(document).ready(),如何以最短的方式实现此行为?

I want to imitate $(document).ready(), How can I implement this behavior in the shortest way?

推荐答案

您可以使用:

document.addEventListener("DOMContentLoaded", function(){
//do things
},false);

请注意,这可能是旧版浏览器中的问题,请参阅 MDN 列表的受支持的浏览器。

for example. Note that this might be a problem in older browsers though, see MDN for a list of supported browsers.

这篇关于如何模仿跨浏览器$(document).ready()行为没有JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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