我试图使 ecHeight 的最小高度等于 custHeightFix 的高度加上一些像素。有人看到我做错了什么吗?我是新手!
var elmnt = document.getElementById("custHeightFix");
var idmHeight = elmnt.offsetHeight;
document.getElementById("ecHeight").style.minHeight = idmHeight +"5000";
最佳答案
您的代码中有两个问题:
idmHeight
为数字;您必须添加数字 (5000
) 而不是字符串 ("5000"
) 才能执行算术运算。如果您添加数字的字符串版本,则会发生字符串连接。 即:18 + "5000"
将产生"185000"
。您还必须在最后指定单位,如
px
。
试试 idmHeight + 5000 + "px"
var elmnt = document.getElementById("custHeightFix");
var idmHeight = elmnt.offsetHeight;
var el = document.getElementById("ecHeight");
el.style.minHeight = idmHeight + 5000 + "px";
el.style.backgroundColor = "lightgray";
console.log(el.style.minHeight);
<div id="custHeightFix">custHeightFix</div>
<div id="ecHeight">ecHeight</div>
关于javascript - 使一个 div min-height 等于另一个 div-height 加上 px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54121999/