Javascript 如何检测元素是否有';自动';高度

Javascript 如何检测元素是否有';自动';高度,javascript,css,height,Javascript,Css,Height,window.getComputedStyle(element).height和element.clientHeight都返回元素的当前高度(以像素为单位),与CSS中设置的值无关 有没有办法确定高度是设置为auto,还是像素以外的其他单位 @pvnarula在他链接的页面中建议的一个解决方案是临时访问。 有点黑…请尝试: document.getElementById("ele_id").style.height 还要检查以下插件: 更新: 基于其他答案和大量在线研究,我想出了一个在单一功

window.getComputedStyle(element).height
element.clientHeight
都返回元素的当前高度(以像素为单位),与CSS中设置的值无关

有没有办法确定高度是设置为
auto
,还是像素以外的其他单位


@pvnarula在他链接的页面中建议的一个解决方案是临时访问。 有点黑…

请尝试:

document.getElementById("ele_id").style.height
还要检查以下插件:


更新:

基于其他答案和大量在线研究,我想出了一个在单一功能中混合所有内容的方法。在这里查看JSFIDLE:

})


使用clone->heightCheck->remove innerHTML->heightCompare方法遇到错误。即使图元具有100%/自动高度,也不会记录高度变化

相反,此方法似乎有效:

let autoHeight = false;
// Set up stage area with 100% height/width
const stage = document.createElement('div');
      stage.setAttribute('style', "position: relative; height: 100%; width: 100%;");
// Add stage to body
document.body.appendChild(stage);
// Clone the element and append to stage
const clone = element.cloneNode(false);
stage.appendChild(clone);
// Get Initial Height
const initialHeight = clone.offsetHeight;
// Squish content
stage.setAttribute('style', "position: relative; height: 1px; width: 1px;");
// Get new height
const currentHeight = clone.offsetHeight;
// Get max height (if it exists)
const hasMaxHeight = getComputedStyle(clone)["maxHeight"];
// Compare
if (currentHeight < initialHeight && hasMaxHeight == 'none') {
  // Has 100% or auto height, and no maxHeight
} else if (hasMaxHeight !== 'none') {
  // Flexible, but has a maxHeight
} else {
  // Constrained by height size
}
// Remove elements
stage.remove();
让autoHeight=false;
//设置高度/宽度为100%的舞台区域
const stage=document.createElement('div');
stage.setAttribute('样式',“位置:相对;高度:100%;宽度:100%;”);
//给身体加舞台
文件.正文.附件(阶段);
//克隆元素并附加到stage
const clone=element.cloneNode(false);
阶段。追加子代(克隆);
//获得初始高度
const initialHeight=clone.offsetHeight;
//压扁物
stage.setAttribute('style','position:relative;height:1px;width:1px;');
//获得新高度
const currentHeight=clone.offsetHeight;
//获取最大高度(如果存在)
const hasMaxHeight=getComputedStyle(克隆)[“maxHeight”];
//比较
如果(currentHeight
这是可行的,但只有通过
style
属性内联应用样式时才有效:(谢谢!我最后在后台克隆了元素,然后比较了高度
// assume false by default
var autoHeight = false;

// clone the div and move it; get its height
var clone = element.clone();
clone.appendTo('#stage');
var initialHeight = clone.height();

// destroy all the content and compare height
clone.html('');
var currentHeight = clone.height();
if (currentHeight &lt; initialHeight) {
    autoHeight = true;
}

// get that clone and its smelly duplicate ID out of the DOM!
clone.remove();
// do the same for the stage
$(&#039;#stage&#039;).remove();

return autoHeight;
let autoHeight = false;
// Set up stage area with 100% height/width
const stage = document.createElement('div');
      stage.setAttribute('style', "position: relative; height: 100%; width: 100%;");
// Add stage to body
document.body.appendChild(stage);
// Clone the element and append to stage
const clone = element.cloneNode(false);
stage.appendChild(clone);
// Get Initial Height
const initialHeight = clone.offsetHeight;
// Squish content
stage.setAttribute('style', "position: relative; height: 1px; width: 1px;");
// Get new height
const currentHeight = clone.offsetHeight;
// Get max height (if it exists)
const hasMaxHeight = getComputedStyle(clone)["maxHeight"];
// Compare
if (currentHeight < initialHeight && hasMaxHeight == 'none') {
  // Has 100% or auto height, and no maxHeight
} else if (hasMaxHeight !== 'none') {
  // Flexible, but has a maxHeight
} else {
  // Constrained by height size
}
// Remove elements
stage.remove();