Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

javascript - 什么是非jQuery等价的'$(document).ready()'?(What is the non-jQuery equivalent of '$(document).ready()'?)

什么是$(document).ready() jQuery等价的$(document).ready()

  ask by TIMEX translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works perfectly, from ECMA(这非常适合ECMA)

document.addEventListener("DOMContentLoaded", function() { // code... }); The window.onload doesn't equal to JQuery $(document).ready because $(document).ready waits only to the DOM tree while window.onload check all elements including external assets and images.(window.onload不等于JQuery $(document).ready因为$(document).ready只等待DOM树,而window.onload检查包括外部资源和图像在内的所有元素。) EDIT : Added IE8 and older equivalent, thanks to Jan Derk 's observation.(编辑 :由于Jan Derk的观察,添加了IE8和更旧的等价物。) You may read the source of this code on MDN at this link :(您可以在此链接上阅读MDN 上此代码的来源:) // alternative to DOMContentLoaded document.onreadystatechange = function () { if (document.readyState == "interactive") { // Initialize your application or run some code. } } There are other options apart from "interactive" .(除了"interactive"之外还有其他选择。) See the MDN link for details.(有关详细信息,请参阅MDN链接。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...