cssRules

     今天学习《javascript高级程序设计》的时候知道了一个小小的知识点。。发现自己原来是这么的菜呀。。。

     做一个demo的时候,发现写在html标签里用style属性的样式<h1 style="width:100px;">,可以用普通的DOM方法获得。例如theEl.style.width,但是如果是写在style标签下的样式<type="text/css">h1{width:100px;}</style>,这种方法就会输出空值。
    这个时候必须用到cssRules集合才能取得、修改该样式的值。

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
< title > css rules </ title >
ExpandedBlockStart.gifContractedBlock.gif
< style  type ="text/css" >
ExpandedSubBlockStart.gifContractedSubBlock.gif#div1
{}{width:100%;height:400px; background:red;}
</ style >
ExpandedBlockStart.gifContractedBlock.gif
< script >

    window.onload
=init;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function $(id){
              
return "string" == typeof id ? document.getElementById(id) : id;
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
function init(){
        
var ocssRules=document.styleSheets[0].cssRules || document.styleSheets[0].rules;
        
var theEl=$("div1");
        ocssRules[
0].style.background="yellow";
        alert(ocssRules[
0].style.background);
    }

    

</ script >
</ head >

< body >
< div  id ="div1" ></ div >
</ body >
</ html >

 

注意:样式集合在IE和FF下的名称是不一样的,FF下是document.styleSheets[0].cssRules

ie下是document.styleSheets[0].rules

转载于:https://www.cnblogs.com/sunjing/archive/2008/11/24/1339903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值