欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

動態(tài)加載外部css或js文件

2022-10-30    分類: 網(wǎng)站建設(shè)

動態(tài)加載外部css或js文件

原理解析:第一步:使用dom創(chuàng)建<script>或者<link>標(biāo)簽,并給他們附加屬性,如type等第二步:使用appendChild方法把標(biāo)簽綁定到另一個標(biāo)簽,一般是綁到<head>.
應(yīng)用:

1、提高代碼的復(fù)用,減少代碼量;

2、添加一個javascript控制器和 session可以實現(xiàn)動態(tài)改變頁面樣式;

3、由于是頁面是從上到下依次加載文件的,并且邊加載邊解釋,所以可以添加javascript控制器控制頁面文件的加載順序,如先加載css布局文件,再顯示有圖片的css美化文件,之后再加載大的falsh文件,或者安內(nèi)容的重要性來加載。

閱讀提示:e文不好的初學(xué)者可以直接看中文,然后拷貝代碼試驗下。

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:

接下來的工作是綁定到<head>標(biāo)簽。綁定的時候有一個問題就是同一個文件有可能被我們綁定兩次,綁定兩次瀏覽器也不會出現(xiàn)異常,但是效率就低了。為了避免

這種情況我們可以新增一個全局?jǐn)?shù)組變量,把綁定的文件名字保存在里面,每次綁定前先檢查一下是否已經(jīng)存在,如果存在就提示已經(jīng)存在,如果不存在就綁定。

以下是引用片段:
document.getElementsByTagName("head")[0].appendChild(fileref)

By referencing the HEAD element of the page first and then calling appendChild(), this means the newly created element is added to the very end of the HEAD tag. Furthermore, you should be aware that no existing element is harmed in the adding of the new element- that is to say, if you call loadjscssfile("myscript.js", "js") twice, you now end up with two new "script" elements both pointing to the same Javascript file. This is problematic only from an efficiency standpoint, as you'll be adding redundant elements to the page and using unnecessary browser memory in the process. A simple way to prevent the same file from being added more than once is to keep track of the files added by loadjscssfile(), and only load a file if it's new:

以下是引用片段:
var filesadded="" //保存已經(jīng)綁定文件名字的數(shù)組變量
function checkloadjscssfile(filename, filetype){
if (filesadded.indexOf("["+filename+"]")==-1){// indexOf判斷數(shù)組里是否有某一項
loadjscssfile(filename, filetype)
filesadded+="["+filename+"]" //把文件名字添加到filesadded
}
else
alert("file already added!")//如果已經(jīng)存在就提示
}
checkloadjscssfile("myscript.js", "js") //success
checkloadjscssfile("myscript.js", "js") //redundant file, so file not added

Here I'm just crudely detecting to see if a file that's set to be added already exists within a list of added files' names stored in variable filesadded before deciding whether to proceed or not.

Ok, moving on, sometimes the situation may require that you actually remove or replace an added .js or .css file. Lets see how that's done next.

以下是引用片段:
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //判斷文件類型
var fileref=document.createElement('script')//創(chuàng)建標(biāo)簽
fileref.setAttribute("type","text/javascript")//定義屬性type的值為text/javascript
fileref.setAttribute("src", filename)//文件的地址
}
else if (filetype=="css"){ //判斷文件類型
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") //打開頁面時瀏覽器動態(tài)的加載文件
loadjscssfile("javascript.php", "js") // 打開頁面時瀏覽器動態(tài)的加載"javascript.php" ,
loadjscssfile("mystyle.css", "css") //打開頁面時瀏覽器動態(tài)的加載.css 文件

網(wǎng)站標(biāo)題:動態(tài)加載外部css或js文件
文章來源:http://aaarwkj.com/news30/209530.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站營銷、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計、用戶體驗、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司
麻豆精品午夜福利在线| 欧美一区二区三区成人网| 日韩不卡免费一区二区三区视频| 欧美亚洲国产另类第一页| 粉嫩av一男战三女高潮| 亚洲精品福利一二三区| 熟女俱乐部五十路六十路| 亚洲av中文久久精品国内| 毛片一区二区三区免费看| 欧美日韩在线一区二区| 国产精品日韩伦理一区二区| 麻豆一精品传二传媒短视频| 91中文字幕在线一区| 视频二区国产欧美日韩| 国产日韩精品欧美综合区| 国产特级黄色片免费看| 国产传媒在线视频观看| 成人免费毛片内射视频| 日本加勒比在线播放一区| 久久精品人妻少妇一区二区| 国产精品盗摄一区二区三区| 国产日韩欧美亚洲中文国| 成人午夜在线免费观看| 欧美一区二区三区情色| 国产精品午夜福利91| 免费观看中国性生活片| 日韩高清中文字幕在线| 门国产av一区二区三区| 人妻熟女一区二区视频| 久久国产福利一区二区| 亚洲av成人三区国产精品| 欧美精品成人免费在线| 国产一级精品自拍视频| 精品视频一区二区三区在线观看| 亚洲视频一区二区精品| 国产av一区二区三区中文| 日韩久久精品国产亚洲av成人| 国产成人自拍激情视频| 在线视频网友自拍偷拍| 麻豆精品国产一区二区91| 日本岛国一区二区在线|