JavaScript新增樣式規(guī)則(推薦)
來源:易賢網(wǎng) 閱讀:748 次 日期:2016-08-01 14:12:22
溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript新增樣式規(guī)則(推薦)”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了JavaScript新增樣式規(guī)則(推薦)的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

關鍵代碼如下所示:

<html>

  <head>

  <link rel="stylesheet" type="text/css" href="basic.css">

    <script>

      window.onload=function(){

           var sheet=document.styleSheets[0];

           //【新增樣式規(guī)則】

          // sheet.insertRule('.div1{font-size:16px;color:red;}',0);     IE8及以上不支持  //第一個參數(shù)是樣式,第二個是樣式的位置

          // sheet.addRule('.div1','font-size:20px;color:orange;font-weight:bold;',0);    //IE都支持    //第一個參數(shù)是樣式名,第二個樣式位置是樣式規(guī)則,第三個是樣式位置

           function insertCss(element,csName,position){                        //跨瀏覽器兼容      

             if(sheet.insertRule){                     // sheet.insertRule 非IE的時候會讀取到一個函數(shù),IE8即以上的時候會讀取到undefined

            sheet.insertRule(element+'{'+csName+'}',position);    

          }else if(sheet.addRule){                      

            sheet.addRule(element,csName,position);

          }

    }

        insertCss('.div1','font-size:16px;color:orange',0);

         //【刪除樣式規(guī)則】

        //sheet.deleteRule(position) ; //刪除樣式 參數(shù)為位置        //IE8及以上不支持

        //sheet.removeRule(position) ; //刪除樣式 參數(shù)為位置        //IE支持

        function deleteCss(position){        //跨瀏覽器兼容

        if(sheet.deleteRule){        

           sheet.deleteRule(position);

      }else if(sheet.removeRule){

          sheet.removeRule(position);

      }  

    deleteCss(0);

    }

}

    </script>

  </head>

  <body></body>

</html>

以上所述是小編給大家介紹的JavaScript新增樣式規(guī)則(推薦),希望對大家有所幫助

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:JavaScript新增樣式規(guī)則(推薦)
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權所有:易賢網(wǎng)