getComputedStyle,getPropertyValue,CurrentStyle

Dom中的getPropertyValue方法可以用来获取元素中指定的css属性值.该方法支持W3C标准.

与IE中的currentStyle方法作用相同.都是根据指定的css属性名称来获取属性值.

区别:

1:getPropertyValue必须配合getComputedStyle方法一起使用.

2:getPropertyValue支持W3C标准.但不支持IE浏览器,

3:currentStyle非W3C标准.只支持IE.不能在FF等浏览器下使用.

var oStyle = this.currentStyle? this.currentStyle : window.getComputedStyle(this, null);

语法:css_value=window.getComputedStyle.getPropertyValue(css_name) 返回值:

css_value:返回对某个css属性值的引用.如:text-align值,position值等.

参数

window.getComputedStyle:直接使用window对象调用getComputedStyle方法来获取所有可用的css属性.
css_name:要获取的css属性值的名称.比如:text-align,position,background等等.

getComputedStyle说明:

Dom中getComputedStyle方法可用来获取元素中所有可用的css属性列表.以数组形式返回.注意啊getComputedStyle不会直接返回元素中某个css样式的属性

值.他返回的是一个数组.这个数组中包括所有可用的css属性.

通常这个方法必须配合getPropertyValue属性使用,才可以获取指定的css属性值,如只想获取width的值或text-align以及left的值.就必须使用getPropertyValue

属性.

语法:arr_style=window.getComputedStyle(elem_id,ov); 返回值:

arr_style:以数组的形式.返回所有的css可用属性.

参数

window:直接调用window对象访问getComputedStyle方法.

elem_id:元素的id,要获取该元素的css样式

ov:伪元素,是否要获取伪元素属性值.如hover,active,link等属性.如果不想获取这些伪元素的属性值请填写为null.

CurrentStyle说明

用来获取元素内Css的style样式属性值,currentStyle仅支持IE浏览器

语法:o=elem.currentStyle[style_name]; 返回值:

o:返回元素某个样式属性值的引用.

参数

elem:要在该元素内获取样式属性.

style_name:样式属性名称.

跨浏览器兼容

function compStyle(elementId, property){

  var element = document.getElementById('elementId');

  var style;

  if(window.getComputedStyle){

    style = window.getComputedStyle(element, null).getPropertyValue(property);

  }else if(element.currentStyle){

    style = element.currentStyle[property];

  }

  return style;

}

原文地址:https://www.cnblogs.com/laborc/p/3066074.html