【问题标题】:How to get css values in IE7 when "auto" is returned返回“auto”时如何在IE7中获取css值
【发布时间】:2011-09-22 20:55:02
【问题描述】:
我正在尝试在 IE7 中读取 div 的高度。 Element.currentStyle 返回“自动”。我现在如何计算这个元素的高度? jQuery 是如何做到这一点的?当 IE 开发人员的栏显示值设置为自动时,它的 height() 函数能够检索值(http://api.jquery.com/height/)?
编辑:我没有使用 jQuery,所以我希望有一个在纯 javascript 中执行此操作的解决方案
【问题讨论】:
标签:
jquery
css
internet-explorer-7
【解决方案1】:
也许document.getElementById("idHere").offsetHeight 有效!
【解决方案2】:
我认为这是计算高度的 jQuery 函数:
function getWH( elem, name, extra ) {
// Start with offset property
var val = name === "width" ? elem.offsetWidth : elem.offsetHeight,
which = name === "width" ? cssWidth : cssHeight;
if ( val > 0 ) {
if ( extra !== "border" ) {
jQuery.each( which, function() {
if ( !extra ) {
val -= parseFloat( jQuery.css( elem, "padding" + this ) ) || 0;
}
if ( extra === "margin" ) {
val += parseFloat( jQuery.css( elem, extra + this ) ) || 0;
} else {
val -= parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0;
}
});
}
return val + "px";
}
// Fall back to computed then uncomputed css if necessary
val = curCSS( elem, name, name );
if ( val < 0 || val == null ) {
val = elem.style[ name ] || 0;
}
// Normalize "", auto, and prepare for extra
val = parseFloat( val ) || 0;
// Add padding, border, margin
if ( extra ) {
jQuery.each( which, function() {
val += parseFloat( jQuery.css( elem, "padding" + this ) ) || 0;
if ( extra !== "padding" ) {
val += parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0;
}
if ( extra === "margin" ) {
val += parseFloat( jQuery.css( elem, extra + this ) ) || 0;
}
});
}
return val + "px";
}