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

本文介绍如何使用JavaScript通过两种不同方法获取DOM元素的样式属性:getPropertyValue()与getPropertyCSSValue()。详细展示了这两种方法的区别,并解释了getPropertyCSSValue()返回的_cssValueType属性的含义。
 1 <body>
 2 <div id="wow" style="font-size:10px; float:left"></div>
 3 
 4 
 5 
 6 <script type="text/javascript">
 7 
 8 var tempDiv=document.getElementById("wow");
 9 var prop_,value,i,len;
10 
11 alert(tempDiv.style.length);
12 
13 //defined the tempDiv's length;
14 for(i=0,len=tempDiv.style.length;i<len;i++)
15 {
16     //the style arrary of tempDiv;
17     prop_=tempDiv.style[i];
18     
19     //Prop_'s propertyValue;
20     value=tempDiv.style.getPropertyValue(prop_);
21     
22     //Prop_'s propertyCSS-Value;
23     _value=tempDiv.style.getPropertyCSSValue(prop_);
24     
25     //alert(prop_+"=>"+value);
26     
27     //alert the properties accrording to the attributes of the _value object.
28     alert(prop_+"=>"+_value.cssText+":"+_value.cssValueType);
29     
30 }
31 
32 
33 </script>
34 
35 
36 
37 
38 
39 
40 
41 </body>

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

代码已经贴出来了,

里面有2个方法:

1.getPropertyValue()

2.getPropertyCSSValue();

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

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

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

 

 

转载于:https://www.cnblogs.com/kmsfan/p/js.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值