读取css文件属性

分两种情况讨论:

1、使用内联样式,即直接把CSS写在HTML元素的style属性中

Html代码 复制代码
  1. <div id="d1" style="width:100px;height:200px;">              
  2. </div>  
<div id="d1" style="width:100px;height:200px;">			
</div>

 通过以下js代码可以获取width等属性,

Js代码 复制代码
  1. alert(document.getElementById('d1').style.width);//100px  

 

在firefox,ie,opera,safari,chrome上都可以取得到宽度和高度值。

 

2、使用嵌入、链入或引入样式表(非内联样式),这时候通过element.style.width是获取不到的。

Html代码 复制代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>b.html</title>      
  5.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  6.     <meta http-equiv="description" content="this is my page">  
  7.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  8.     <style type="text/css">  
  9.         #d2 {   
  10.             width:400px;   
  11.             height:200px;   
  12.             border:5px solid gray;   
  13.             padding:5px;   
  14.         }   
  15.     </style>       
  16.     <script>  
  17.         window.onload=function(){   
  18.                
  19.             //2、使用嵌入、链入或引入样式表(非内联样式)   
  20.                
  21.             //ie:通过currentStyle            
  22.             alert(document.getElementById('d2').currentStyle.width);//400px ie   
  23.                
  24.             //ff,safari,opera,chrome:通过window.getComputedStyle   
  25.             var el=document.getElementById('d2');   
  26.             alert(window.getComputedStyle(el,null).style.width);//400px safari,opera,chrome   
  27.         }   
  28.     </script>  
  29.   </head>  
  30.      
  31.   <body>         
  32.         <div id="d2"></div>  
  33.   </body>  
  34. </html>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>b.html</title>	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <style type="text/css">
    	#d2 {
    		width:400px;
			height:200px;
			border:5px solid gray;
			padding:5px;
    	}
    </style>    
	<script>
		window.οnlοad=function(){
			
			//2、使用嵌入、链入或引入样式表(非内联样式)
			
			//ie:通过currentStyle			
			alert(document.getElementById('d2').currentStyle.width);//400px ie
			
			//ff,safari,opera,chrome:通过window.getComputedStyle
			var el=document.getElementById('d2');
			alert(window.getComputedStyle(el,null).style.width);//400px safari,opera,chrome
		}
	</script>
  </head>
  
  <body>		
		<div id="d2"></div>
  </body>
</html>

 

最后封装到一个方法中,

Js代码 复制代码
  1. function getRealStyle(el,styName)   
  2. {   
  3.     var len=arguments.length;   
  4.     var sty=null;   
  5.     if('currentStyle' in el){   
  6.         sty=el.currentStyle;   
  7.     }else if('getComputedStyle' in window){   
  8.         sty=window.getComputedStyle(el,null);   
  9.     }   
  10.     if(len==1){   
  11.         return sty;    
  12.     }else if(len==2){   
  13.         return sty[styName];   
  14.     }   
  15. }  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值