窗口加载里面的文件准备好了吗?

发布于 2024-10-17 19:09:04 字数 645 浏览 8 评论 0原文

抱歉,如果之前已经回答过这个问题,但所有搜索都讨论了差异,而不是如果可能的话,将两者一起使用。

简单地说,可以在 $(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

任性一次 2024-10-24 19:09:04

这工作得很好并且是一种可以接受的做法。毕竟,正如您所描述的,在某些情况下,$(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.

腹黑女流氓 2024-10-24 19:09:04

我最近遇到了这个问题...从 jQuery 版本 3 开始,我们不能再将 $(window).on('load') 放在 document.ready() 中...因为就绪处理程序是异步调用,也就是说ready可以在加载后调用。

来自 jQuery 核心团队Github:jQuery 3 问题

需要明确的是,我们了解造成这种情况的原因。我们最近做好了准备
处理程序异步触发。这具有难以达到的优点
放弃。缺点是就绪处理程序有时会触发
如果加载事件触发得足够快,则在加载事件之后。侧面
您在本期中看到的效果是您绑定了一个加载事件
加载事件已经触发后的处理程序。

修复方法是将负载绑定到ready之外。

<前><代码>$(函数() {
// 文档准备好后需要发生的事情
});

$(窗口).on("加载", function() {
// 满载后需要发生的事情
});

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

To be clear, we understand what's causing this. We recently made ready
handlers fire asynchronously. This has advantages that are hard to
give up. The disadvantage is that the ready handler can sometimes fire
after the load event if the load event fires quickly enough. The side
effect you're seeing in this issue is that you're binding a load event
handler after the load event has already fired.

The fix is to bind the load outside of ready.

$(function() {
   // Things that need to happen when the document is ready
});

$(window).on("load", function() {
   // Things that need to happen after full load
});
属性 2024-10-24 19:09:04

编辑:

注意:此答案仅适用于 jQuery v2 及更低版本。


jQuery .ready() 事件

传递给.ready()的处理程序保证被执行
DOM 准备就绪后,因此这通常是附加所有其他事件处理程序并运行其他 jQuery 代码的最佳位置。

jQuery .load() 事件方法

当元素和所有子元素都具有时,load 事件将发送到元素
已完全加载
。该事件可以发送到任何元素
与 URL 关联:图像、脚本、框架、iframe 和
窗口对象。

根据上面的 jQuery 文档,我没有看到任何表明以下问题的内容:

$(document).ready(function () {
    // will fire IMMEDIATELY after the DOM is constructed

    $(window).load(function() {
        // will only fire AFTER all pages assets have loaded
    })

});

.load 放在 ready 中只是保证 DOM 已准备就绪每当 load 被触发时。

人们可能希望将所有 jQuery 代码放在单个 DOM 就绪处理程序中,但仍然可能有一个需要首先加载所有图像的 jQuery 代码子集。这种安排保证了所有代码在 DOM 准备好后立即触发,其余代码将在页面资源加载完成时随后触发。

这可能最终更多地是个人喜好的问题,但是OP明确询问这种安排是否会引起问题。这尚未被证明是正确的。

EDIT:

NOTE: This answer is only applicable to jQuery v2 and below.


jQuery .ready() event:

The handler passed to .ready() is guaranteed to be executed
after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.

jQuery .load() event method:

The load event is sent to an element when it and all sub-elements have
been completely loaded
. This event can be sent to any element
associated with a URL: images, scripts, frames, iframes, and the
window object.

Based on the jQuery documentation above, I've seen nothing that would indicate any issues with the following:

$(document).ready(function () {
    // will fire IMMEDIATELY after the DOM is constructed

    $(window).load(function() {
        // will only fire AFTER all pages assets have loaded
    })

});

Placing a .load inside of a ready simply guarantees that the DOM is ready whenever the load 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.

忱杏 2024-10-24 19:09:04

警告
这个问题的答案已经非常过时了。

正如 @ViRuSTriNiTy 在评论中提到的,此代码在 jQuery 3 中不再有效,并且它被作为 GitHub

所以不再建议使用这种方法。

一种方法是使用以下代码。

$(window).on("load", function(){
   $.ready.then(function(){
      // Both ready and loaded
   });
})

但是,如果您加载就绪图像并希望在完全加载后调用一些代码,则此代码将不起作用。

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

$(window).on("load", function(){
   $.ready.then(function(){
      // Both ready and loaded
   });
})

However, this code won't work if you load images in ready and want to call some code after it is fully loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文