18禁裸体动漫美女无遮挡网站-国产精品人妻一码二码尿失禁-伊人久久亚洲精品一区-小说区 图片区色 综合区-人妻无码av一区二区三区精品

深一集團(tuán)40%的客戶來自外地,80%的業(yè)務(wù)來自搜索引擎和良好口碑!
微信 微信人工客服  |
微信人工客服
QQ在線溝通

新聞中心

全國(guó)業(yè)務(wù)咨詢請(qǐng)致電

0755-27612861

為您打響品牌第一炮
  • 在線QQ
  • 在線客服
  • 在線留言
動(dòng)態(tài)加載外部css或js文件
發(fā)布日期:2008-12-24   關(guān)鍵詞:鳳崗英文網(wǎng)站開發(fā),鳳崗大型網(wǎng)絡(luò)公司   已有 7093 人瀏覽

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

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

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

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

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

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

      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)簽。綁定的時(shí)候有一個(gè)問題就是同一個(gè)文件有可能被我們綁定兩次,綁定兩次瀏覽器也不會(huì)出現(xiàn)異常,但是效率就低了。為了避免

      這種情況我們可以新增一個(gè)全局?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ù)組里是否有某一項(xiàng)
  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") //打開頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載文件
loadjscssfile("javascript.php", "js") // 打開頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載"javascript.php" ,
loadjscssfile("mystyle.css", "css") //打開頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載.css 文件


 

注:本文來自深一集團(tuán)原創(chuàng)或轉(zhuǎn)截 http://m.hefeihsyc.com/newslist_635_2.html 如需轉(zhuǎn)載,請(qǐng)注明出處!
0
深一網(wǎng)絡(luò)公司專注設(shè)計(jì)14年
全國(guó)網(wǎng)站建設(shè)
深一云服務(wù)器深一云服務(wù)器
高性能,高安全
網(wǎng)絡(luò)公司拒絕不當(dāng)利
崇尚野蠻生長(zhǎng)
500強(qiáng)企業(yè)網(wǎng)500強(qiáng)企業(yè)網(wǎng)
站建設(shè)供應(yīng)商
10000家客戶案例10000家客戶案
實(shí)力說服力
83位技術(shù)團(tuán)隊(duì)83位技術(shù)團(tuán)隊(duì)
服務(wù)高保障
深一只做有排名網(wǎng)站只做有排名
有價(jià)值的網(wǎng)站
200人服務(wù)團(tuán)隊(duì)200人服務(wù)團(tuán)隊(duì)
追求客戶滿意
主站蜘蛛池模板: 中文人妻av久久人妻18| 亚洲欧美国产va在线播放 | 亚洲国产精品一区二区制服| 人妻熟妇乱又伦精品无码专区| 日本无码欧美一区精品久久| 强伦姧人妻三上悠亚中文字幕 | 亚洲性夜夜综合久久7777| 亚洲精品揄拍自拍首页一| 国产精品美女乱子伦高潮| 首页 综合国产 亚洲 丝袜日本| 国产国拍亚洲精品av| 欧美精品v国产精品v日韩精品| 精品少妇xxxx| 丰满白嫩人妻中出无码| 天堂网在线最新版www| 欧美黑人粗大猛烈18p| 国产又爽又黄又爽又刺激| 无码人妻少妇精品无码专区漫画 | 色综合久久精品亚洲国产| 无码东京热一区二区三区| 狠狠亚洲狠狠欧洲2019| 久久久久久久曰本精品免费看| 国产白浆喷水在线视频| 精品人妻中文无码av在线| 澳门永久av免费网站| 亚洲中文字幕无码av网址| 无码热综合无码色综合| 四虎国产精品免费久久| 99久久99久久加热有精品| 国产成人无码精品xxxx| 亚洲鲁丝片av无码多人| 国产精品久久久久久久久鸭无码| 5d肉蒲团之性战奶水| 亚洲一区二区三区播放| 男人和女人高潮免费网站| 亚洲国产精品va在线看黑人| 久久久无码中文字幕久...| 国产成人无码综合亚洲日韩| 97精品人人a片免费看| 欧美超大胆裸体xx视频| 国产办公室秘书无码精品99|