当前位置:Gxlcms >
html代码 >
动态加载js和css的jqueryplugin_html/css_WEB-ITnose
动态加载js和css的jqueryplugin_html/css_WEB-ITnose
时间:2021-07-01 10:21:17
帮助过:28人阅读
一个简单的动态加载js和css的jquery代码,用于在生成页面时通过js函数加载一些共通的js和css文件。
Java代码
- //how to use the function below:
- //$.include('file/ajaxa.js');$.include('file/ajaxa.css');
- //or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);(only if .js and .css files are in the same directory)
- $.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' " : " type='text/javascript' ";
- var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
- if ($(tag + "[" + link + "]").length == 0) $("head").prepend("<" + tag + attr + link + ">" + tag + ">");
- }
- }
- });
- $.include('../js/jquery-ui-1.8.21.custom.min.js');
- $.include('../css/black-tie/jquery-ui-1.8.21.custom.css');
将该函数写入一个common.js文件中,在html中加载该common.js文件,就可以达到目的。该js函数出自以下链接:
http://www.cnblogs.com/chenjinfa/archive/2009/03/17/1414178.html
注意:
1.在html5中,