我正在使用 javascript 来显示当前 div 的高度。
这是受影响区域的示例
//css
.test
{
height:1px;
}
#test1
{
margin:1px;
}
//html
<div id="test1" class="test"></div>
//javascript
var a = document.getElementById('test1');
a.style.height //how I access the style
Firebug 说 style 的长度为 0,高度为空。 如何获得正确的高度?
最佳答案
您需要查看计算的样式,而不是指定的样式。参见 Quirks mode's getstyle回答问题的页面
Sometimes you'll want to see what styles the default document view has. For instance, you gave a paragraph an width of 50%, but how do you see how many pixels that is in your users' browser?
它解释了如何派生和使用 getstyle
函数,尽管使用像 jquery 这样提供简单 css 的库更容易。功能。
关于Javascript 样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6880636/