请提供洞察这个谜。使用document.getElementById()从css获取值。style.height javascript
我试图通过
var high = document.getElementById("hintdiv").style.height;
alert(high);
摆脱一个div框高度值,如果属性包含div标签里我能得到这个值就好了,但它返回如果属性为空值在css部分中定义。
这很好,它显示100px为一个值。该值可以被访问。
<div id="hintdiv" style="height:100px; display: none;">
.
.
var high = document.getElementById("hintdiv").style.height;
alert(high);
这不好,它显示一个空的警报屏幕。该值实际上是0。
#hintdiv
{
height:100px
display: none;
}
<div id="hintdiv">
.
.
var high = document.getElementById("hintdiv").style.height;
alert(high);
但是我没有问题访问/改变“显示:无”属性是否是在该标签或在CSS部分。 div框通过属性定义方法(标签内或CSS部分)正确显示。
我也试图通过其他的变化来访问值,但没有运气
document.getElementById("hintdiv").style.height.value ----> undefined
document.getElementById("hintdiv").height ---->undefined
document.getElementById("hintdiv").height.value ----> error, no execution
任何解决方案?
TIA。
请参阅http://stackoverflow.com/questions/1098349/reading-non-inline-css-style-info-from-javascript和http://stackoverflow.com/questions/1048336/cant-access-css- selectors-properties-from-javascript – 2010-06-16 01:51:50
另请参阅:http://gist.github.com/369133和http://stackoverflow.com/questions/2531737/javascript-incapable-of-getting-elements-max-height-via -element-style-maxheight/ – CMS 2010-06-16 02:14:42
谢谢大家,这当然是意想不到的。我想我会为这个1(或2个元素)做内联样式。 – Jamex 2010-06-16 02:26:27