js的getComputedStyle('')[attr]

 

DOM标准引入了覆盖样式表的概念,当我们用document.getElementById("id").style.backgroundColor 获取样式时 获取的只是id中style属性中设置的背景色,如果id中的style属性中没有设置background-color那么就会返回空,也就是说如果 id用class属性引用了一个外部样式表,在这个外部样式表中设置的背景色,那么不好意思document.getElementById("id").style.backgroundColor 这种写法不好使 ,如果要获取外部样式表中的设置,需要用到window对象的getComputedStyle()方法,代码这样写window.getComputedStyle(id,null).backgroundColor 
但是兼容问题又来了,这么写在firefox中好使,但在IE中不好使
两者兼容的方式写成

Js代码  收藏代码
  1. window.getComputedStyle?window.getComputedStyle(id,null).backgroundColor:id.currentStyle["backgroundColor"];  
 


如果是获取背景色,这种方法在firefox和IE中的返回值还是不一样的,IE中是返回"#ffff99"样子的,而firefox中返回"rgb(238, 44, 34) "
值得注意的是:window.getComputedStyle(id,null)这种方式不能设置样式,只能获取,要设置还得写成类似这样id.style.background="#EE2C21";

 

首先我们先说一下样式表属性
1. 内联样式即元素style属性里面设置的,级别最高
2. 页面样式表定义即页面<style></style>里面定义的,级别次之
3.外部链接样式表文件
JavaScript获取和设置文档元素的css属性:
1.获取元素Style属性里面设置的样式属性,
document.getElementById(id).style.height;
有,则返回属性值;没有则返回空
IE和火狐皆然,只是有的属性值返回可能不一样,比如像颜色火狐返回rgb,而IE是返回十六进制数字
测试代码:

Js代码  收藏代码
  1. <script type="text/javascript">  
  2. function getCss(){  
  3. alert(document.getElementById("aaa").style.height);  
  4. alert(document.getElementById("aaa").style.backgroundColor);  
  5. alert(document.getElementById("aaa").style.width);  
  6. document.getElementById("aaa").style.backgroundColor = ‘#dbdbdb';  
  7. //alert(myWidth);  
  8. }  
  9. </script>  
 

 

Html代码  收藏代码
  1. <div id="aaa" class="bbb" style="height:20px; ">  
  2. asdfasdfas  
  3. </div>  
  4. <input type="button" value="点击" onclick="getCss();" />  
 


2.有时候我们的样式可能有多个地方设置了,我们也不知道它到底是外部样式表属性起作品,还是在内联样式里面起作用,所以我们就需要获取当前页面渲染的属性值。这个在IE和FF里面有些不同:
示例代码片断:
IE:document.getElementById('aaa').currentStyle.height
FF标准:document.defaultView.getComputedStyle(aaa,null).getPropertyValue('height')
这两个属性是只读的,若要改变元素样式还得使用style,它直接写在元素style属性里面级别最高起作用
3.写一个兼容IE和FF的函数来调用
复制代码 代码如下:

Js代码  收藏代码
  1. function getRealStyle(id,styleName) {  
  2. var element = document.getElementById(id);  
  3. var realStyle = null;  
  4. if (element.currentStyle)  
  5. realStyle = element.currentStyle[styleName];  
  6. else if (window.getComputedStyle)  
  7. realStyle = window.getComputedStyle(element,null)[styleName];  
  8. return realStyle;  
  9. }  

 

调用:cur_height = parseInt(getRealStyle(CON_ID,'height'));
P.S:返回值通常会带有单位,需要使用parseInt函数提取数字,以方便后面的操作

js中style,currentStyle和getComputedStyle的区别以及获取css操作方法


一、style,currentStyle和getComputedStyle的区别
<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
  #p1 {width: 100px;height: 200px;color: #09c;}
 </style>
</head>
 
<body>
<p id="p1" >HHHH</p>
<script>
 function getStyle(obj, attr)  
{  
 
    if(obj.currentStyle)  
    {  
       return obj.currentStyle[attr];  //只适用于IE
    }  
    else  
    {  
       return getComputedStyle(obj,false)[attr];   //只适用于FF,Chrome,Safa
    }  
return obj.style[attr]; //若内联样式为<p id="p1" style="margin:10px;">HHHH</p>则这样可获取,若行间无style标签则返回空
}  
 
 window.onload=function()  
{  
//调用
    var oDiv=document.getElementByIdx_x('p1');  
//alert(getStyle(oDiv,'width'));
alert(getStyle(oDiv,'margin-left'));
//查询了相关资料发现问题如下:
//style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的。 
//currentStyle可以弥补style的不足,但是只适用于IE。
//getComputedStyle同currentStyle作用相同,但是适用于FF、opera、safari、chrome。
//
//注意
//currentStyle和getComputedStyle只能用于获取页面元素的样式,不能用来设置相关值。
//如果要设置相应值,应使用style。
 
</script>
</body>
</html>
 
二、获取css操作方法
1.用JS修改标签的 class 属性值:document.getElementByIdx_x( "tt" ).className = "txt"; 
2.用JS修改标签的 style 属性值:document.getElementByIdx_x( "t2" ).style.color = "red"; 
posted @ 2016-11-11 09:34  it蛰伏者  阅读(755)  评论(0)    收藏  举报