这样做是可行的:
这样做是而不是的工作:
最奇怪的是,如果我使用javascript设置宽度:
警报( document.getElementById("hello").style.width );document.getElementById("hello").style.width = "25%";
会管用的。第一个警报将显示空白警报,然后第二个警报将显示"25%“。
发布于 2012-03-06 15:32:57
您不能以未使用Javascript设置的方式访问CSS属性。您需要使用getComputedStyle,请参阅https://developer.mozilla.org/en/DOM/window.getComputedStyle在MSIE上的工作方式。
发布于 2012-03-06 15:40:09
尝试以下几点
window.onload = function(){
var x = document.getElementById("hello");
var y ="";
if (x.currentStyle)
    y = x.currentStyle['width'];
else if (window.getComputedStyle)
    y = document.defaultView.getComputedStyle(x,null).getPropertyValue('width');
alert(y);
};它的灵感来源于我读到的这里
发布于 2012-03-06 15:30:24
在InternetExplorer中,您可以使用.currentStyle而不是.style。在其他浏览器中,您可以使用getComputedStyle机制。
var yourElement = document.getElementById('whatever');
var theStyle = window.getComputedStyle(yourElement);然后,可以对返回的样式对象调用.getPropertyValue(),以查找您感兴趣的CSS属性。
https://stackoverflow.com/questions/9586615
复制相似问题