【发布时间】:2018-06-06 16:09:22
【问题描述】:
我必须获取少数元素的高度(例如:.content-one > .title)并为所有项目设置最高的数字。
<div class="content-one">
<h3 class="title">Section One</h3>
</div>
<div class="content-two">
<h3 class="title">Section Two</h3>
</div>
这是我写的代码。
maxHeight('.content-one');
maxHeight('.content-two');
function maxHeight(element) {
const contents = document.querySelectorAll(element);
contents.forEach(function(content) {
var title = content.getElementsByClassName('title')[0];
if (title.clientHeight > 20) {
title.style.height = 40 + "px";
console.log("done");
}
});
}
在这里,它检查高度并尝试设置高度。但它只为高度 > 20 的元素设置高度。对此最好的解决方案是什么?
【问题讨论】:
-
为什么 40 是这里的“最高数字”?如果这显然不是您想要的,为什么还有
if? -
@Luca 这是我找到的最佳解决方案。我想设置最高值。
标签: javascript foreach height