JS 窗口就绪和脚本计时
超级简单的一个。使用 Mootools 或 JQuery 或普通的旧 JS 之类的东西。您能否在窗口准备好之前选择要处理的节点
例如(对不起,我的速记)
<script>
my = $('main');
my.danceLevel = 1000000;
WindowReady(){
dance(my);
}
</script>
由于窗口尚未准备好,您可以指望元素得到证实吗?窗口是否准备好渲染或正在解析等......?
Super easy one. Using something like Mootools or JQuery or just plain old JS. Can you select a node to work on before the window being ready
For instance (sry for my short hand)
<script>
my = $('main');
my.danceLevel = 1000000;
WindowReady(){
dance(my);
}
</script>
Since the window is not ready can you count on elements being substantiated? Is window ready it being ready to render or being parsed etc..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设“main”是体内的一个对象,答案是否定的。 “我的= $('主');”将返回未定义。
Assuming that "main" is an object in the body, the answer is no. "my = $('main');" will return undefined.
不,元素不会在那里,因此您无法在头部或元素渲染之前引用它们。
需要在 jQuery 文档准备就绪 的情况下执行此操作,或者在渲染元素之后放置脚本。
No, elements will not be there so you can not reference them in in the head or before the element has been rendered.
Need to do it with jQuery document ready or place the script after the elements have been rendered.
你可以这样做,但你真的不应该......
为什么你不能仅仅依靠你选择的 JS 库的 DOM Ready 功能呢?除非页面标记很大,否则 DOM 准备就绪所需的时间与此方法所获得的速度增益最多是最小的。这只会给你的脚本增加不必要的复杂性。
You can do this, but you really shouldn't...
Why can't you just rely on the DOM Ready function of your JS Library of choice? Unless the page markup is GIGANTIC, the time it takes for the DOM to be ready vs the speed gain from this method will be minimal at best. This will just add unnecessary complexity to your scripts.