您的位置:首页 > Web前端 > JavaScript

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

2013-11-13 14:56 681 查看
<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(自定义的值)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐