这次给大家带来js/css动态加载js插件,制作js/css动态加载js插件的注意事项有哪些,下面就是实战案例,一起来看一下。
JavaScript创建节点添加到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到<body>...</body>cr_node("http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js");//添加jquery到<head>...</head>cr_node("http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js","head");//添加jquery到<div id="id">...</div>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());
}
})相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
以上就是js/css动态加载JS插件的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号