Possible Duplicate:
Get a CSS value with JavaScript
你能告诉我为什么我在加载页面时无法获取这些CSS属性吗?
javascript:
function aviso(){
alert("top: "+document.getElementById("divRojo").style.top);
alert("position: "+document.getElementById("divRojo").style.position);
}
HTML:
<html>
<head>
<style type="text/css">
#divRojo {
width:100px;
height:100px;
background:red;
left:200px;
top:200px;
position:static;
}
</style>
</head>
<body onload="aviso()">
<div id="divRojo">
Div Rojo
</div>
</body>
</html>
提前致谢
最佳答案
这是因为 DOMElement.style
属性包含内联样式 - 它不包含通过 CSS 应用的样式规则。
使用getComputedStyle
获取应用的样式:
var el = document.getElementById("divRojo");
alert(window.getComputedStyle(el).top);
alert(window.getComputedStyle(el).position);
关于javascript - 为什么我在加载页面时无法获取这些 css 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13504524/