此扩展可以更好的隐藏好自己写的JS和CSS文件,代码原理很简单,通俗易懂,话不多说看主要代码展示吧:
扩展代码如下:
- $.extend({
- includePath: '',
- include: function(file) {
- var files = typeof file == "string" ? [file]:file;
- for (var i = 0; i < files.length; i++) {
- var name = files[i].replace(/^\s|\s$/g, "");
- var att = name.split('.');
- var ext = att[att.length - 1].toLowerCase();
- var isCSS = ext == "css";
- var tag = isCSS ? "link" : "script";
- var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
- var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
- if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
- }
- }
- });
页面使用代码如下:
- $.includePath = 'http://www.jq-school.com/js/';
- $.include(['manhua_date.js', 'jquery.lazyload.js', 'style.css']);
如果您觉得本文的内容对您的学习有所帮助:
关键字:
实现网页动态加载js和css文件的jquery扩展