窗口加载里面的文件准备好了吗?
抱歉,如果之前已经回答过这个问题,但所有搜索都讨论了差异,而不是如果可能的话,将两者一起使用。
简单地说,可以在 $(document).ready.(function() {})
内部使用 $(window).load.(function() {})
吗?
我有一些事情应该在 DOM 加载后完成,但我想仅在图像加载完成后才显示某些元素。在 Explorer 8 中唯一有效的是将 $(window).load
函数与其他所有内容一起放入 $(document).ready
中。
这是可以接受的做法吗?
我只想在完全构建后使用最可接受的方法来显示包含小图像(例如工具栏)的 DIV
。 (例如,可见性
隐藏
与显示
无
)。此 DIV
的 HTML 由 $(document).ready
内的代码编写,然后使用 $('body').append 附加到正文()
在使用 $(window).load
之前。
我在使用 Explorer 8 时遇到很多问题。
Sorry if this has been answered before but all searches talk about the differences, not about using the two together, if possible.
Simply, can $(window).load.(function() {})
be used INSIDE of $(document).ready.(function() {})
?
I have some things that should be done after the DOM is loaded but then I want to reveal certain elements only after the images have finished loading. The only thing that works in Explorer 8, is putting the $(window).load
functions inside of the $(document).ready
with everything else.
Is this an acceptable practice?
I just want to use the most acceptable method to reveal a DIV
that contains small images, like a toolbar, after it's fully constructed. (visibility
hidden
versus display
none
, for example). The HTML for this DIV
is written by the code inside the $(document).ready
and then appended to the body using $('body').append()
before using the $(window).load
.
I'm having lots of problems with Explorer 8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这工作得很好并且是一种可以接受的做法。毕竟,正如您所描述的,在某些情况下,$(window).load() 处理程序中的逻辑可能取决于 DOM 准备好后发生的工作。如果在您设置 $(window).load() 时窗口实际上已经加载,则处理程序将立即触发。
This works fine and is an acceptable practice. After all, as you describe, there may be cases where the logic in the
$(window).load()
handler depends on work taking place after the DOM is ready. If the window is in fact already loaded by the time you set up$(window).load()
, the handler will just fire immediately.我最近遇到了这个问题...从 jQuery 版本 3 开始,我们不能再将 $(window).on('load') 放在 document.ready() 中...因为就绪处理程序是异步调用,也就是说ready可以在加载后调用。
来自 jQuery 核心团队:Github:jQuery 3 问题
I ran into this problem recently... from jQuery version 3, we can no longer put $(window).on('load') inside document.ready()... because ready handler are called asynchronously, which means ready can be called after load.
From jQuery Core Team: Github: jQuery 3 issues
编辑:
注意:此答案仅适用于 jQuery v2 及更低版本。
jQuery
.ready()
事件:jQuery
.load()
事件方法:根据上面的 jQuery 文档,我没有看到任何表明以下问题的内容:
将
.load
放在ready
中只是保证 DOM 已准备就绪每当load
被触发时。人们可能希望将所有 jQuery 代码放在单个 DOM 就绪处理程序中,但仍然可能有一个需要首先加载所有图像的 jQuery 代码子集。这种安排保证了所有代码在 DOM 准备好后立即触发,其余代码将在页面资源加载完成时随后触发。
这可能最终更多地是个人喜好的问题,但是OP明确询问这种安排是否会引起问题。这尚未被证明是正确的。
EDIT:
NOTE: This answer is only applicable to jQuery v2 and below.
jQuery
.ready()
event:jQuery
.load()
event method:Based on the jQuery documentation above, I've seen nothing that would indicate any issues with the following:
Placing a
.load
inside of aready
simply guarantees that the DOM is ready whenever theload
is fired.One may wish to place all jQuery code within a single DOM ready handler, and yet still may have a sub-set of jQuery code that requires all images be loaded first. This arrangement guarantees that all code be triggered once on DOM ready and the rest will be triggered subsequently whenever the page assets have finished loading.
This may ultimately be more of an issue of personal preference, however the OP was clearly asking if this arrangement would cause problems. This has not proven to be true.
警告:
这个问题的答案已经非常过时了。
正如 @ViRuSTriNiTy 在评论中提到的,此代码在 jQuery 3 中不再有效,并且它被作为 GitHub。
所以不再建议使用这种方法。
一种方法是使用以下代码。
但是,如果您加载就绪图像并希望在完全加载后调用一些代码,则此代码将不起作用。
WARNING:
The answers in this question are quite outdated.
As @ViRuSTriNiTy mentioned in comments that this code is no longer valid in jQuery 3 and it was mentioned as an issue on GitHub.
So this method is not advised anymore.
One way to do it is to use the following code
However, this code won't work if you load images in ready and want to call some code after it is fully loaded.