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

加載 jQuery 插件后執(zhí)行某些操作

Do something once jQuery plugin has loaded(加載 jQuery 插件后執(zhí)行某些操作)
本文介紹了加載 jQuery 插件后執(zhí)行某些操作的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在將 jQueryjQuery UI 動(dòng)態(tài)加載到頁(yè)面中,我需要知道 jQuery UI 何時(shí)成功擴(kuò)展了 jQuery.

目前我正在使用加載 jQuery UI 的腳本元素的 readystate 來(lái)觸發(fā)我的代碼的運(yùn)行,但我認(rèn)為那時(shí),腳本已加載,但 jQuery UI 尚未正確初始化.

在定義 $.ui 之前是否是唯一的輪詢選擇?

這是我目前正在處理的代碼:

(load_ui = (callback) ->script2 = document.createElement("腳本")script2.type = "文本/javascript"script2.src = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.js"script2.onload = script2.onreadystatechange = ->console.log 'readystate:', @readystate如果@readystate ==加載"或@readystate ==完成"console.log "jquery ui 已加載"回調(diào) ($ = window.jQuery).noConflict(1), done = 1$(script,script2).remove()document.documentElement.childNodes[0].appendChild script2console.log '附加到頁(yè)面的 jquery ui 腳本元素'(窗口、文檔、req_version、回調(diào)、$、腳本、完成、就緒狀態(tài))->如果不是 ($ = window.jQuery) 或 req_version >;$.fn.jquery 或回調(diào)($)console.log "開(kāi)始加載 jquery"腳本 = document.createElement("腳本")script.type = "文本/javascript"script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + req_version + "/jquery.min.js"script.onload = script.onreadystatechange = ->如果未完成并且 (not (readystate = @readyState) 或 readystate == "loaded" 或 readystate == "complete")console.log "jquery 已加載,現(xiàn)在正在加載 jquery ui"load_ui(回調(diào))document.documentElement.childNodes[0].appendChild 腳本console.log '附加到頁(yè)面的 jquery 腳本元素') 窗口、文檔、1.6.1"、($, L) ->控制臺(tái)日志 $控制臺(tái).log $.ui.version

由于某種原因,jquery ui 腳本元素的 readystate 只返回 undefined.

解決方案

我認(rèn)為這將完全滿足您的需求,盡可能多地添加依賴.

(function () {函數(shù)getScript(網(wǎng)址,成功){var script = document.createElement('script');腳本.src = 網(wǎng)址;var head = document.getElementsByTagName('head')[0];var已完成=假;script.onload = script.onreadystatechange = function () {if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {完成=真;成功();script.onload = script.onreadystatechange = null;head.removeChild(腳本);}};head.appendChild(腳本);}getScript("Scripts/jquery-1.6.1.js", function () {getScript("腳本/jquery-ui-1.8.11.js", function () {警報(bào)($.ui);});});})();

<塊引用>

測(cè)試 IE7、IE8、IE9、Firefox、Safari、Chrome 和 Opera

I'm dynamically loading jQuery and jQuery UI into a page, and I need to know when jQuery UI has successfully extended jQuery.

At the moment I'm using the readystate of the script element that loads jQuery UI to trigger the running of my code, but I think that at that point, the script has loaded, but jQuery UI hasn't been properly initialised.

Is the only choice to poll until $.ui is defined?

Here's the code that I'm currently wrestling with:

(load_ui = (callback) ->
    script2 = document.createElement("script")
    script2.type = "text/javascript"
    script2.src = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.js"
    script2.onload = script2.onreadystatechange = ->
      console.log 'readystate:', @readystate
      if @readystate == "loaded" or @readystate == "complete"
        console.log "jquery ui is loaded"
        callback ($ = window.jQuery).noConflict(1), done = 1
        $(script,script2).remove()
    document.documentElement.childNodes[0].appendChild script2
    console.log 'jquery ui script element appended to page'
(window, document, req_version, callback, $, script, done, readystate) ->
  if not ($ = window.jQuery) or req_version > $.fn.jquery or callback($)
    console.log "begin loading jquery"
    script = document.createElement("script")
    script.type = "text/javascript"
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + req_version + "/jquery.min.js"
    script.onload = script.onreadystatechange = ->
      if not done and (not (readystate = @readyState) or readystate == "loaded" or readystate == "complete")
        console.log "jquery is loaded, now loading jquery ui"
        load_ui(callback)        

    document.documentElement.childNodes[0].appendChild script
    console.log 'jquery script element appended to page'
) window, document, "1.6.1", ($, L) ->
    console.log $
    console.log $.ui.version

For some reason the readystate of the jquery ui script element just returns undefined.

解決方案

I think this will do exactly what you need, add more dependency as much as you like.

(function () {
        function getScript(url, success) {
            var script = document.createElement('script');
            script.src = url;

            var head = document.getElementsByTagName('head')[0];
            var completed = false;
            script.onload = script.onreadystatechange = function () {
                if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
                    completed = true;
                    success();
                    script.onload = script.onreadystatechange = null;
                    head.removeChild(script);
                }
            };
            head.appendChild(script);
        }

        getScript("Scripts/jquery-1.6.1.js", function () {
            getScript("Scripts/jquery-ui-1.8.11.js", function () {
                alert($.ui);
            });
        });
})();

Tested with IE7, IE8, IE9, Firefox, Safari, Chrome and Opera

這篇關(guān)于加載 jQuery 插件后執(zhí)行某些操作的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創(chuàng)建頭像的 jQuery/JavaScript 庫(kù)?)
How to do following mask input problem?(如何做以下掩碼輸入問(wèn)題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設(shè)置值/標(biāo)簽的問(wèn)題)
how to unit-test private methods in jquery plugins?(如何對(duì) jquery 插件中的私有方法進(jìn)行單元測(cè)試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動(dòng)網(wǎng)站配置偏移量/對(duì)齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當(dāng)文本框獲得焦點(diǎn)時(shí)選擇所有內(nèi)容)
主站蜘蛛池模板: 欧美日韩一区二区三区四区五区 | 最新中文字幕在线 | 国产精品不卡一区 | 久久久久久久久久久久久久国产 | 国产精品欧美一区二区三区 | 国产黄色小视频在线观看 | 九九成人| 免费看黄色国产 | 国产原创视频 | 看av片网站| 男女污网站 | 成人免费高清 | 午夜小影院 | 一区二区三区四区不卡视频 | 国产免费视频在线 | 免费av播放 | 在线观看av网站永久 | 国产一级毛片精品完整视频版 | 九九热精品视频 | 精品久久久一区 | 在线观看av网站 | 成人自拍视频网站 | 日韩伦理电影免费在线观看 | 一区二区三区精品在线视频 | 中文字幕不卡在线观看 | 精品99在线 | 中文字幕一区二区三区四区五区 | 玖玖国产 | 一区二区免费看 | 精品欧美一区二区精品久久久 | 国产精品成人一区二区三区 | 久久精品性视频 | 天天干干 | 国产精品久久国产精品99 | 国产在线观 | av无遮挡| 99热激情| 男人天堂网址 | 北条麻妃一区二区三区在线视频 | 久草新在线| 欧美性a视频 |