亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        使用javascript插入樣式

        字號(hào):


            這篇文章主要介紹了使用javascript插入樣式的相關(guān)方法,用javascript插入<style>樣式以及<link>樣式,感興趣的小伙伴們可以參考一下
            一、用javascript插入<style>樣式
            有時(shí)候我們需要利用js來(lái)動(dòng)態(tài)生成頁(yè)面上style標(biāo)簽中的css代碼,方法很直接,就是直接創(chuàng)建一個(gè)style元素,然后設(shè)置style元素里面的css代碼,最后把它插入到head元素中。
            但有些兼容性問(wèn)題我們需要解決。首先在符合w3c標(biāo)準(zhǔn)的瀏覽器中我們只需要把要插入的css代碼作為一個(gè)文本節(jié)點(diǎn)插入到style元素中即可,而在IE中則需要利用style元素的styleSheet.cssText來(lái)解決。
            還需要注意的就是在有些版本IE中一個(gè)頁(yè)面上style標(biāo)簽數(shù)量是有限制的,如果超過(guò)了會(huì)報(bào)錯(cuò),需要考慮這點(diǎn)。
            function addCSS(cssText){
             var style = document.createElement('style'), //創(chuàng)建一個(gè)style元素
              head = document.head || document.getElementsByTagName('head')[0]; //獲取head元素
             style.type = 'text/css'; //這里必須顯示設(shè)置style元素的type屬性為text/css,否則在ie中不起作用
             if(style.styleSheet){ //IE
              var func = function(){
               try{ //防止IE中stylesheet數(shù)量超過(guò)限制而發(fā)生錯(cuò)誤
                style.styleSheet.cssText = cssText;
               }catch(e){
               }
              }
              //如果當(dāng)前styleSheet還不能用,則放到異步中則行
              if(style.styleSheet.disabled){
               setTimeout(func,10);
              }else{
               func();
              }
             }else{ //w3c
              //w3c瀏覽器中只要?jiǎng)?chuàng)建文本節(jié)點(diǎn)插入到style元素中就行了
              var textNode = document.createTextNode(cssText);
              style.appendChild(textNode);
             }
             head.appendChild(style); //把創(chuàng)建的style元素插入到head中 
            }
            //使用
            addCSS('#demo{ height: 30px; background:#f00;}');
            當(dāng)然這只是一個(gè)最基本的演示方法,實(shí)際運(yùn)用中還需進(jìn)行完善,比如把每次生成的css代碼都插入到一個(gè)style元素中,這樣在IE中就不會(huì)發(fā)生stylesheet數(shù)量超出限制的錯(cuò)誤了。
            封裝:
            代碼如下:
            var importStyle=function importStyle(b){var a=document.createElement("style"),c=document;c.getElementsByTagName("head")[0].appendChild(a);if(a.styleSheet){a.styleSheet.cssText=b}else{a.appendChild(c.createTextNode(b))}};
            importStyle('h1 { background: red; }');//調(diào)用
            seajs封裝
            代碼如下:
            seajs.importStyle=function importStyle(b){var a=document.createElement("style"),c=document;c.getElementsByTagName("head")[0].appendChild(a);if(a.styleSheet){a.styleSheet.cssText=b}else{a.appendChild(c.createTextNode(b))}};
            二、javascript插入<link>樣式
            在<head>中使用<link>標(biāo)簽引入一個(gè)外部樣式文件,這個(gè)比較簡(jiǎn)單,各個(gè)主流瀏覽器也不存在兼容性問(wèn)題:
            function includeLinkStyle(url) {
            var link = document.createElement(“l(fā)ink”);
            link.rel = “stylesheet”;
            link.type = “text/css”;
            link.href = url;
            document.getElementsByTagName(“head”)[0].appendChild(link);
            }
            includeLinkStyle(“http://css.xxx.com/home/css/reset.css?v=20101227”);
            以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。