javascript操作CSS样式表

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html>
 3  <head>
 4   <meta charset="UTF-8" />
 5   <!--meta http-equiv="x-ua-compatible"content="ie=7"/--> 
 6   <title>css</title>
 7   <style type="text/css">
 8       #div0{
 9          width:100px;
10          height:100px;
11          background:red;
12       }
13   </style>
14   
15   <script type="text/javascript">
16       window.onload = function(){
17          var oDiv = document.getElementById("div0");
18 
19          var sheet = document.styleSheets[0];
20          var ruls = sheet.cssRules || sheet.rules;
21          var rule = ruls[0];
22          alert(rule.selectorText);
23          alert(rule.style.cssText);
24          alert(rule.style.background);
25 
26          if(document.all)
27          {
28             sheet.addRule("#div0","color:blue",0);
29          }else
30          {
31             sheet.insertRule("#div0{color:orange}",0)
32          }
33          
34 
35       }
36   </script>
37  </head>
38  <body>
39     <div id="div0">DIV</div>
40  </body>
41 </html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。