JavaScript 获取HTML中的CSS样式的属性以及值的的方法。

时间:2022-06-01 23:21:52
 <body>
<div id="wow" style="font-size:10px; float:left"></div> <script type="text/javascript"> var tempDiv=document.getElementById("wow");
var prop_,value,i,len; alert(tempDiv.style.length); //defined the tempDiv's length;
for(i=0,len=tempDiv.style.length;i<len;i++)
{
//the style arrary of tempDiv;
prop_=tempDiv.style[i]; //Prop_'s propertyValue;
value=tempDiv.style.getPropertyValue(prop_); //Prop_'s propertyCSS-Value;
_value=tempDiv.style.getPropertyCSSValue(prop_); //alert(prop_+"=>"+value); //alert the properties accrording to the attributes of the _value object.
alert(prop_+"=>"+_value.cssText+":"+_value.cssValueType); } </script> </body>

PS:注释是我写的,因为我一般习惯写英文注释了,不太喜欢写中文参杂在代码里。

代码已经贴出来了,

里面有2个方法:

1.getPropertyValue()

2.getPropertyCSSValue();

这2个方法里的属性大家可以对比一下。

其中getPropertyCSSValue()中的属性:cssValueType是一个存放常量的枚举类型:

Alert出的结果:0(继承的值) 1(基本的值) 2(自定义的值)