将div的高度设置为0

时间:2021-02-04 04:59:28

标签: javascript html dom

我动态生成了以下 HTML 代码。我需要将高度为 heightdiv34px 设置为 0px。由于此 div 没有 classid,因此我无法使用任何 DOM 操作选项,例如 .getElementById().getElementByClassName 或 {{ 1}}。我需要使用 .getElementByTag() 将此 heightdiv 设置为 0

enter image description here

编辑

我很抱歉,我应该以更好的方式提出问题,并且我应该在提出问题之前提前预料并了解需求。这才是我真正需要的:

JavaScript

您的回答对我有帮助,但是由于所有父 <div class="fht-cell"> <div class="filterControl"> <!-- This should stay as it is. --> </div> </div> <div class="fht-cell"> <div style="height: 34px;"> <!-- This height should become 0px. --> </div> </div> <div class="fht-cell"> <div class="filterControl"> <!-- This should stay as it is. --> </div> </div> <div class="fht-cell"> <div style="height: 34px;"> <!-- This height should become 0px. --> </div> </div> 都有 div 类,而且由于我们提到了 child fht-cell 仅第一个 [0] 的高度变成了div

2 个答案:

答案 0 :(得分:1)

您可以使用 document.getElementsByClassName('fht-cell')[0].children[0] 或类似的。

答案 1 :(得分:0)

first-child 选择器应该可以解决问题。 https://www.w3schools.com/cssref/sel_firstchild.asp