非 jquery 文档就绪 - window.onload 是最好/唯一的方法吗?
我希望在文档准备就绪时运行一个函数,但没有合理的理由利用 jquery,因为页面上进行的少量工作不保证我加载该大小的文件。
通常我会使用 window.onload = function(){};
——据我所知,这是推荐的方法。
想法/建议?如果有更好的方法,我非常希望听到一些开发人员的意见。
注意-
如果这个问题最好在其他地方问(因为它有点主观)-请告诉我,我会关闭它并在那里询问。
I want a function to run when the document
is ready, but there's no logical reason to leverage jquery as the little amount of work going on the page doesn't warrant me loading a file of that size.
Typically I would use window.onload = function(){};
-- as far as I can tell, this is the recommended approach.
Thoughts/recommendations? I would very much like to hear some developer opinions on this if there's a better way.
Note-
If this question is better asked elsewhere (because it's somewhat subjective) - please let me know and I'll close it and ask there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
window.onload
的问题是它将等待所有内容(如图像)加载。通常,您只需要准备好 DOM 即可进行操作。您应该知道,jQuery 消除了许多跨浏览器的不一致性,因此您自己这样做将是一个小麻烦。因此,您可能需要研究 这个库,它包含跨浏览器 DOM 准备的基本框架。The problem with
window.onload
is that it will wait for all content (like images) to be loaded. Typically you only need for the DOM to be ready to be manipulated. You should know though that jQuery eliminates a lot of the cross browser inconsistencies in doing that, and as such doing it yourself will be a small pain in the ass. For that reason, you might want to look into this library which contains the bare bones of cross-browser DOM readiness.如果您想为非 JQuery“domready”事件提供通用支持的方法,可以选择创建一个应在 DOM 准备就绪时执行的函数,并添加
文档末尾的标签:
If you want to have an universal supported method for a non-JQuery "domready" event, an option is to create a function which should be executed when the DOM is ready, and add a
<script>
tag at the end of the document:编写相当于 jQuery.ready 事件的内容。 jQuery 只是 javascript,因此您没有理由不能创建类似的事件。它只是为了减少您使代码浏览器兼容的工作量。
Write the equivalent of jQuery.ready event. jQuery is just javascript, so there is no reason you couldn't make a similar event. It is just there to reduce you're work at making your code browser compatible.
您可以将
onload
添加到页面上的body
标记中。做类似的事情:
You could add an
onload
to yourbody
tag on the page.Do something like: