</pre>为了得到css的属性,通过style.属性名称,只能得到在style中设置的属性值,为了得到外联的css以及写在外部的css样式,可以用下面的代码<p></p><p></p><pre name="code" class="html">var b=document.getElementById("b");
if(b.currentStyle) k=b.currentStyle["width"];
else
k=window.getComputedStyle(b,null)["width"];
currentStyle是在ie浏览器中可用
jquery的源码里面用的是
document.defaultView
这个跟window差不多,在FireFox3.6上得用defaultView
方法还访问框架(frame)的样式
getComputedStyle
var style = window.getComputedStyle(element[, pseudoElt]);
后面是伪类,没有的话就填null,:Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 之前是必须参数,现在不是了
除了上面的格式外还可以通过
k=window.getComputedStyle(b,null).getPropertyValue("width");
在ie浏览器中还可以
k=b.currentStyle.getAttribute("width");
alert(k);