国产精品性一区二区三区,奇米狠狠色,日韩在线第二页,欧美人与动牲交片免费,日本免费人成视频播放,亚洲国产一区二区三区网,俺也去色官网,丝袜AV在线一区二区三区
              深一集團40%的客戶來自外地,80%的業務來自搜索引擎和良好口碑!
              微信 微信人工客服  |
              微信人工客服
              QQ在線溝通

              新聞中心

              全國業務咨詢請致電

              0755-27612861

              為您打響品牌第一炮
              • 在線QQ
              • 在線客服
              • 在線留言
              動態加載外部css或js文件
              發布日期:2008-12-24   關鍵詞:鳳崗英文網站開發,鳳崗大型網絡公司   已有 7424 人瀏覽

              動態加載外部css或js文件

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

                1、提高代碼的復用,減少代碼量;

                2、添加一個javascript控制器和 session可以實現動態改變頁面樣式;

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

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

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

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

              以下是引用片段:
              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="" //保存已經綁定文件名字的數組變量
              function checkloadjscssfile(filename, filetype){
              if (filesadded.indexOf("["+filename+"]")==-1){// indexOf判斷數組里是否有某一項
                loadjscssfile(filename, filetype)
                filesadded+="["+filename+"]" //把文件名字添加到filesadded
              }
              else
                alert("file already added!")//如果已經存在就提示
              }
              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')//創建標簽
                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") //打開頁面時瀏覽器動態的加載文件
              loadjscssfile("javascript.php", "js") // 打開頁面時瀏覽器動態的加載"javascript.php" ,
              loadjscssfile("mystyle.css", "css") //打開頁面時瀏覽器動態的加載.css 文件


               

              注:本文來自深一集團原創或轉截 http://m.ywsxmw.cn/newslist_635_2.html 如需轉載,請注明出處!
              0
              深一網絡公司專注設計14年
              全國網站建設
              深一云服務器深一云服務器
              高性能,高安全
              網絡公司拒絕不當利
              崇尚野蠻生長
              500強企業網500強企業網
              站建設供應商
              10000家客戶案例10000家客戶案
              實力說服力
              83位技術團隊83位技術團隊
              服務高保障
              深一只做有排名網站只做有排名
              有價值的網站
              200人服務團隊200人服務團隊
              追求客戶滿意
              主站蜘蛛池模板: 久久精品国产屋| 国产白袜脚足j棉袜在线观看| 亚洲高清一区二区三区在线观看| 亚洲无码性爱视频在线观看 | 国产一级在线现免费观看| 久久精品无码专区东京热| 亚洲乱码日产精品bd在线| 无码日韩精品一区二区人妻| 色偷偷一区二区无码视频| 999人在线精品播放视频| 亚洲国产综合专区在线电影| 青青草原国产精品啪啪视频| 久久久久无码中文字幕| 国产精品女丝袜白丝袜| 亚洲美女av日韩一区| 亚洲精品日本久久久中文字幕| 日本一区二区在线免费观看| 脱岳裙子从后面挺进去视频| 国产97色在线 | 国| 国产精品亚洲综合色区丝瓜| 偏关县| 日韩精品一区二区三区在线观看的 | 黑巨人与欧美精品一区| 北岛玲精品一区二区三区| 日本高清日本在线免费| 日本一区二区三区专区 | 日本少妇高潮喷水XXXXXXX| 亚洲天天综合色制服丝袜在线| 亚洲精品久久久中文字| 免费人成网上在线观看网址| 纯肉无遮挡H肉动漫在线观看国产| 无码免费午夜福利片在线| 少妇搡bbbb搡bbb搡野外| 色橹橹欧美在线观看视频高清| 久久国产热这里只有精品| 视频一区二区三区中文字幕狠狠| 毛片无码高潮喷白浆视频| 丰满日本少妇一二三区| 亚洲一久久久久久久久| 在线天堂新版最新版在线8| 中文字幕久久精品波多野结百度|