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

查看:24
本文介绍了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() 之前执行的;是execute,所以感觉真的很奇怪.现在我必须将我的代码放在 .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?

推荐答案

NOTE: .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();

另一方面,只要 Web 浏览器能够运行您的 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();

当您离开页面时将触发此事件.这可能是刷新/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() 将在 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天全站免登陆