久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么在切換到 jQuery 3 后我的“加載"事件

Why is my #39;load#39; event/function not beeing executed after switching to jQuery 3?(為什么在切換到 jQuery 3 后我的“加載事件/函數沒有被執行?)
本文介紹了為什么在切換到 jQuery 3 后我的“加載"事件/函數沒有被執行?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

由于我已經從 jQuery 1.x/jQuery 2.x 升級到 jQuery 3.x,我現有的代碼不會不再正確執行.一切正常,但 load 事件偵聽器不再被觸發,或者只是有時:

Since I've upgraded from jQuery 1.x / jQuery 2.x to jQuery 3.x, my existing code will not be executed correctly anymore. Everything works fine, but the load event listener gets not triggered anymore or just sometimes:

$(function() {
    $(window).on("load", function() {
        // this line will never/occasionally be executed
        console.log("window is loaded!");
    });
});

推薦答案

使用/切換到 jQuery 3 時可能會出現問題.這是因為新的 jQuery 3 中的所有 ready states 現在都是完全的 asynchr.這意味著,您的代碼沒有給定的執行順序.

The problem can be occur when using/switching to jQuery 3. It's because all ready states in the new jQuery 3 are now fully asynchron. This means, that there is no given order for your code to be executed.

因此,可能會發生 load 被觸發 before 您的 ready state 已被執行.當你的 ready 函數現在終于被觸發時,你的 load 監聽器為時已晚,不會被執行.

Because of this, it could happen, that load is been triggered before your ready state has been executed. When your ready function now finally gets triggered, your load listener is too late and will not be executed.

jQuery 用法:

要更改此行為,只需刪除 load 事件偵聽器初始化周圍的 ready 狀態.不需要用 ready 函數來封裝它.你可以不初始化它們.

To change this behavior, just remove the ready state around your load event listener initialization. There is no need to encapsulate this with a ready function. You can initialize them without.

// $(function() {
    $(window).on("load", function() {
        // this line will now be executed again
        console.log("window is loaded!");
    });
// });

如果您需要或想要同時注冊這兩個事件,您可以自己注冊 load 事件,并在 ready 狀態 內決定下一步該做什么.

If you need or want to register both events, you can register the load event by yourself and decide inside the ready state what to do next.

// track the loading state by yourself
var windowLoaded = false;
$(window).on("load", function() {
    windowLoaded = true;
});

$(function() {
    function afterLoad() {
        console.log("loaded");
    }

    // decide inside your ready state what to do
    if( !windowLoaded ) {
        $(window).on("load", afterLoad);
    }
    else {
        afterLoad();
    }
});

<小時>

jQuery 插件:

另一種情況是 jQuery 插件,它也使用 load 事件.例如:

Another case would be jQuery plugins, that uses the load event too. For example:

(function($, window) {
    $.fn.myPlugin = function() {
        $(window).on("load", start);

        function start() {
            console.log("plugin initialized and window loaded");
        }
    };
})(jQuery, window);

如果開發人員/用戶現在將插件初始化包裝在 ready 狀態,問題可能會再次發生,就像之前解釋的那樣:

If a developer/user now wraps the plugin initialization in a ready state the problem could happen again, just like explained before:

$(function() {
    $("#element").myPlugin();
});

一種解決方案是自行跟蹤插件中的 load 事件,以打破 就緒狀態.

A solution would be to track the load event in your plugin on your own, to break out the ready state.

(function($, window) {
    // track the loading state beside the plugin initialization
    var windowLoaded = false;
    $(window).on("load", function() {
        windowLoaded = true;
    });

    $.fn.myPlugin = function() {
        // decide inside your plugin how to start
        if( !windowLoaded ) {
            $(window).on("load", start);
        }
        else {
            start();
        }

        function start() {
            console.log("plugin initialized and window loaded");
        }
    };
})(jQuery, window);

<小時>

結論:

即使你沒有遇到這個問題,在你的測試中,你應該在使用 jQuery 3 時立即更改這些代碼,因為其他用戶/不同的瀏覽器可能會遇到這個問題.其他人可能會遇到問題,因為它是異步,你永遠無法知道你的代碼何時/是否被執行...

Even when this problem not happens to you, in your tests, you should change those code immediately, when using jQuery 3, because other users/different browser can run into this trouble. Others may got the problem, because it is asynchron, you could never know when/if your code gets executed ...

這篇關于為什么在切換到 jQuery 3 后我的“加載"事件/函數沒有被執行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 久久毛片网站 | 九九九久久国产免费 | 国产精品久久久久久福利一牛影视 | av一区二区在线观看 | 99久久久久国产精品免费 | 日本午夜精品 | 日本手机在线 | 911精品国产 | 亚洲网视频 | 久久精品国产一区 | 深夜福利影院 | 爱爱综合网| 九九免费视频 | 18gay男同69亚洲网站 | 日本高清视频在线播放 | 日本黄色影片在线观看 | 羞羞视频在线观看网站 | 91免费电影 | 国产欧美精品一区二区 | 精品乱码一区二区三四区视频 | 99福利视频 | 91久久精品一区二区二区 | 国产免费一二三区 | av手机在线播放 | 视频一区在线观看 | 久久av一区 | 亚洲三区在线 | 日韩欧美高清dvd碟片 | 国产日韩视频 | 综合五月 | 91精品国产综合久久香蕉麻豆 | 一区二区三区四区免费观看 | 超碰在线影院 | 黄色男女网站 | 成人av片在线观看 | 奇米av| 自拍偷拍小视频 | 四虎影院在线观看av | 中文字幕一区二区三区日韩精品 | 成年人在线播放 | 亚洲一二三区免费 |