一個簡單的動態(tài)加載js和css的jquery代碼
一個簡單的動態(tài)加載js和css的jquery代碼,用于在生成頁面時通過js函數(shù)加載一些共通的js和css文件。
//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');
將該函數(shù)寫入一個common.js文件中,在html中加載該common.js文件,就可以達(dá)到目的。
注意:
1.在html5中,<script>標(biāo)簽已經(jīng)不支持language屬性了,所以我刪除了:
var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
中的language='javascript'
2.原作者在寫入js和css標(biāo)簽時,用的是:
document.write("<" + tag + attr + link + "></" + tag + ">");
但是經(jīng)過實踐,發(fā)現(xiàn)document.write()方法會在寫入前清除原頁面的所有內(nèi)容,也就相當(dāng)于覆蓋的意思,這樣明顯達(dá)不到我的需要,我需要在加載頁面時動態(tài)的向頁面導(dǎo)入共通的js和css,而不能清除我原頁面的其他任何內(nèi)容,所以查了下api,我改用了:
$("head").prepend("<" + tag + attr + link + "></" + tag + ">");
這個方法,$("head").prepend()方法的作用是在<head>標(biāo)簽的最前端追加寫入內(nèi)容。
最后,再補(bǔ)充一個方法,也是通過共通js來實現(xiàn),應(yīng)該比上面這個方法更容易理解:
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
- 使用jQuery動態(tài)加載js腳本文件的方法
- jquery及js實現(xiàn)動態(tài)加載js文件的方法
- jquery getScript動態(tài)加載JS方法改進(jìn)詳解
- 三種動態(tài)加載js的jquery實例代碼另附去除js方法
- 如何使用jquery動態(tài)加載js,css文件實現(xiàn)代碼
- jquery動態(tài)加載js/css文件方法(自寫小函數(shù))
- 使用jquery動態(tài)加載Js文件和Css文件
- 使用jquery動態(tài)加載javascript以減少服務(wù)器壓力
- jquery動態(tài)加載js三種方法實例
- 使用jquery動態(tài)加載js文件的方法
- jQuery實現(xiàn)動態(tài)加載(按需加載)javascript文件的方法分析
相關(guān)文章
jQuery移除tr無效的解決方法(tr是動態(tài)添加)
移除掉某些tr(tr是動態(tài)添加的)嘗試了很多方法,都不見效,后來發(fā)現(xiàn)個不錯的方法,于是與大家分享下2014-09-09Jquery實現(xiàn)鼠標(biāo)移動放大圖片功能實例
這篇文章主要介紹了Jquery實現(xiàn)鼠標(biāo)移動放大圖片功能,實例分析了jQuery操作鼠標(biāo)與圖片的技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03jQuery Ajax async=>false異步改為同步時,解決導(dǎo)致瀏覽器假死的問題
今天小編就為大家分享一篇jQuery Ajax async=>false異步改為同步時,解決導(dǎo)致瀏覽器假死的問題,具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧2019-07-07jQuery實現(xiàn)可拖拽3D萬花筒旋轉(zhuǎn)特效
本文主要介紹了使用了CSS3立體效果的強(qiáng)大特效,本特效使用jQuery跟CSS3 transform來實現(xiàn)在用戶鼠標(biāo)按下拖動時,環(huán)形圖片墻可以跟隨鼠標(biāo)進(jìn)行3D旋轉(zhuǎn)動畫。下面跟著小編一起來看下吧2017-01-01