使一个div最小高度等于另一个div高度加px

时间:2019-01-10 04:32:41

标签: javascript

我正在尝试使ecHeight的最小高度等于custHeightFix的高度加上一定数量的像素。有人看到我在做什么错吗?我是新来的!

var elmnt = document.getElementById("custHeightFix");
var idmHeight = elmnt.offsetHeight; 
document.getElementById("ecHeight").style.minHeight = idmHeight +"5000";

1 个答案:

答案 0 :(得分:1)

您的代码中有两个问题:

  1. idmHeight是数字;您必须添加数字(5000而不是字符串("5000")才能执行算术运算。如果添加数字的字符串版本,则会发生字符串串联。 即,18 + "5000"将产生"185000"

  2. 还必须在末尾指定类似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>

相关问题