jQuery事件.load(),.ready(),.unload() [英] jQuery events .load(), .ready(), .unload()

查看:167
本文介绍了jQuery事件.load(),.ready(),.unload()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题,对于jQuery事件。当加载DOM时,是否按顺序运行.load(),.ready()和.unload()?当我看到jQuery文档时,答案似乎是肯定的。

Just a simple question, for the jQuery event. Are the .load(), .ready() and .unload() run in order when the DOM is loaded? The answer seems yes when I see the jQuery Documentation.

<script type="text/javascript">

    $(window).load(function () {
        // run code
        initializeCode();
    });

    $(document).ready(function() {
        //run code that MUST be after initialize
    });

    $(window).unload(function() {
        Cleanup();
    });
</script>

然而,.ready()中的代码在initializeCode()之前执行;是执行,所以我觉得很奇怪。现在我必须将代码放在.onload()方法之内,并且在initializeCode()之后)。行,这意味着在.ready()块内。

However, the code inside the .ready() is execute before the initializeCode(); is execute, so I feel really strange. And now I have to place my code inside the .onload() method and just after the initializeCode(); line, which means to be inside the .ready() block.

有人可以为我解释一下这个,因为我是jQuery的新手?

Could someone explain me more about this, as I am new to jQuery?

推荐答案

注意: .load()& .unload()已被已弃用






NOTE: .load() & .unload() have been deprecated


$(window).load();

将在页面之后执行所有内容完成加载。这意味着所有图像,CSS(和CSS定义的内容,如自定义字体和图像),脚本等都被加载。当您的浏览器的停止-icon变为灰色时,会发生事件触发。这对检测文档及其所有内容何时加载非常有用。

Will execute after the page along with all its contents are done loading. This means that all images, CSS (and content defined by CSS like custom fonts and images), scripts, etc. are all loaded. This happens event fires when your browser's "Stop" -icon becomes gray, so to speak. This is very useful to detect when the document along with all its contents are loaded.

$(document).ready();

另一方面,只要网络浏览器能够运行JavaScript,在解析器完成DOM之后发生。如果您想尽快执行JavaScript,这很有用。

This on the other hand will fire as soon as the web browser is capable of running your JavaScript, which happens after the parser is done with the DOM. This is useful if you want to execute JavaScript as soon as possible.

$(window).unload();

当您从页面导航时,此事件将被触发。这可以是Refresh / F5,按前一页按钮,导航到另一个网站或关闭整个标签/窗口。

This event will be fired when you are navigating off the page. That could be Refresh/F5, pressing the previous page button, navigating to another website or closing the entire tab/window.

总而言之,ready()将被触发before load()和unload()将是最后被触发。

To sum up, ready() will be fired before load(), and unload() will be the last to be fired.

这篇关于jQuery事件.load(),.ready(),.unload()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆