>  기사  >  php教程  >  jquery를 사용하여 js, css 파일을 동적으로 로드하는 방법

jquery를 사용하여 js, css 파일을 동적으로 로드하는 방법

高洛峰
高洛峰원래의
2016-11-24 09:47:521163검색

jquery를 사용하여 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://hi.baidu.com/javascript/’;
$.include([&#39;json2.js&#39;, &#39;jquery.tree.js&#39;, &#39;jquery.tree.css&#39;]);


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.