>  기사  >  웹 프론트엔드  >  js 및 css_jquery를 동적으로 로드하기 위한 간단한 jquery 코드

js 및 css_jquery를 동적으로 로드하기 위한 간단한 jquery 코드

WBOY
WBOY원래의
2016-05-16 16:38:041037검색

페이지 생성 시 js 함수를 통해 일부 일반적인 js 및 css 파일을 로드하는 데 사용되는 js 및 css를 동적으로 로드하기 위한 간단한 jquery 코드입니다.

//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 &#63; "link" : "script"; 
var attr = isCSS &#63; " type='text/css' rel='stylesheet' " : " type='text/javascript' "; 
var link = (isCSS &#63; "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 파일에 작성하고 common.js 파일을 html로 로드하여 목표를 달성하세요.
참고:
1. html5에서는 3f1c4e4b6b16bbbd69b2ee476dc4f83a 태그가 더 이상 언어 속성을 지원하지 않으므로 삭제했습니다.

var attr = isCSS &#63; " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";


의 언어='javascript' 2. 원저자는 js와 css 태그를 작성할 때 다음을 사용했습니다:

document.write("<" + tag + attr + link + "></" + tag + ">");
$("head").prepend("<" + tag + attr + link + "></" + tag + ">");
이 메서드인 $("head").prepend() 메서드는 93f0f5c25f18dab9d176bd4f6de5d30e 태그의 프런트 엔드에 콘텐츠를 추가하는 데 사용됩니다.

마지막으로 일반 js를 통해 구현되는 또 다른 메서드를 추가합니다. 위의 메서드보다 이해하기 쉽습니다.

Dynamically loading external JavaScript and CSS files 

To load a .js or .css file dynamically, in a nutshell, it means using DOM methods to first create a swanky new "SCRIPT" or "LINK" element, assign it the appropriate attributes, and finally, use element.appendChild() to add the element to the desired location within the document tree. It sounds a lot more fancy than it really is. Lets see how it all comes together: 

function loadjscssfile(filename, filetype){ 
if (filetype=="js"){ //if filename is a external JavaScript file 
var fileref=document.createElement('script') 
fileref.setAttribute("type","text/javascript") 
fileref.setAttribute("src", filename) 
} 
else if (filetype=="css"){ //if filename is an external CSS file 
var fileref=document.createElement("link") 
fileref.setAttribute("rel", "stylesheet") 
fileref.setAttribute("type", "text/css") 
fileref.setAttribute("href", filename) 
} 
if (typeof fileref!="undefined") 
document.getElementsByTagName("head")[0].appendChild(fileref) 
} 

loadjscssfile("myscript.js", "js") //dynamically load and add this .js file 
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file 
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file

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