js/css动态加载JS插件

这次给大家带来js/css动态加载js插件,制作js/css动态加载js插件的注意事项有哪些,下面就是实战案例,一起来看一下。

注意事项添加到body或head

添加js的代码:

var n = document.createElement("script");n.setAttribute("type", "text/javascript");n.setAttribute("src", i);document.body.appendChild(n); document.head.appendChild(n);

登录后复制

写个用来动态添加节点js的代码:

/*es6 中 函数设置默认参数可以使用 例:function 函数名(变量= 默认值) {...}如果想要兼容可以使用 例:function 函数名(变量) {if(变量==undefined){变量= 默认值}....}*/function cr_node(i, l = "body") { //创建节点并添加    t = i.split(".").reverse()[0];//获取后缀    var n = null;    if (t == "js") {//后缀判断        n = document.createElement("script");        n.setAttribute("type", "text/javascript");        n.setAttribute("src", i);    } else if (t == "css") {        n = document.createElement("link");        n.setAttribute("rel", "stylesheet");        n.setAttribute("href", i);    }    if (n != null) {        if (l == "body") {            document.body.appendChild(n);        } else if (l == "head") {            document.head.appendChild(n);        } else {            l.appendChild(n);        }    }    return n;}

登录后复制

注意事项:

立即学习“前端免费学习笔记(深入)”;

//添加bootstrap样式cr_node("http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css","head")//默认添加jquery到...cr_node("http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js");//添加jquery到...cr_node("http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js","head");//添加jquery到
...
cr_node("http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js",document.getElementById("id"))

登录后复制

2.添加绑定事件函数

添加单个节点并绑定 事件

function cr_node(i, l = "body") {....}function addNode(o, c = undefined) { //单个添加    var n = null;    if(typeof(o) == "object") {        n = cr_node(o.src, (o.parent ? o.parent : "body"));        if(typeof(o.load) == "function") {            n.onload = o.load; //绑定加载事件        }        if(typeof(o.err) == "function") {            n.onerror = o.err; //绑定错误事件        }    } else if(typeof(o) == "string") {        n = cr_node(o); //直接添加节点        if(typeof(c) == "function") {            n.onerror = n.onload = c; //绑定加载事件        }    }}

登录后复制

使用说明:

//直接使用addNode("http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js",function(){...});//完整使用addNode({    "src": "http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js",    "load": function() {        console.log("加载成功");        console.log($("body").html());    },    "err":function() {        console.log("加载失败");    },    "parent": document.getElementById("id")//不写默认是body})

登录后复制

3.尝试写个批量添加的函数

如果仅仅是添加可以使用以下函数:

function addNodes(o, index = 0) { //多个添加    if (o.src.length > index && typeof(o.src) == "object" && o.src.length > 0) {        var n = cr_node(o.src[index], o.parent);        if (o.src.length - 1 == index && typeof(o.load) == "function") {            n.onerror = n.onload = o.load;        } else {            n.onerror = n.onload = function() {                addNodes(o, index + 1);            }        }    }}

登录后复制

使用示例:

addNodes({ "src":["http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css","http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js","http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js","http://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"],"parent":"head", "load":function(){ console.log(1111);   console.log($("body").html()); }})

登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!

相关阅读:

注意事项

注意事项

注意事项

以上就是js/css动态加载JS插件的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2784017.html

(0)
上一篇 2025年3月8日 16:58:38
下一篇 2025年3月8日 16:58:49

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论