加载 jQuery 插件后执行某些操作

2023-01-26前端开发问题
3

本文介绍了加载 jQuery 插件后执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在将 jQueryjQuery UI 动态加载到页面中,我需要知道 jQuery UI 何时成功扩展了 jQuery.

目前我正在使用加载 jQuery UI 的脚本元素的 readystate 来触发我的代码的运行,但我认为那时,脚本已加载,但 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 已加载"回调 ($ = window.jQuery).noConflict(1), done = 1$(script,script2).remove()document.documentElement.childNodes[0].appendChild script2console.log '附加到页面的 jquery ui 脚本元素'(窗口、文档、req_version、回调、$、脚本、完成、就绪状态)->如果不是 ($ = window.jQuery) 或 req_version >;$.fn.jquery 或回调($)console.log "开始加载 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 已加载,现在正在加载 jquery ui"load_ui(回调)document.documentElement.childNodes[0].appendChild 脚本console.log '附加到页面的 jquery 脚本元素') 窗口、文档、1.6.1"、($, L) ->控制台日志 $控制台.log $.ui.version

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

解决方案

我认为这将完全满足您的需求,尽可能多地添加依赖.

(function () {函数getScript(网址,成功){var script = document.createElement('script');脚本.src = 网址;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 () {警报($.ui);});});})();

<块引用>

测试 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

这篇关于加载 jQuery 插件后执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455