我动态生成了以下 HTML
代码。我需要将高度为 height
的 div
的 34px
设置为 0px
。由于此 div
没有 class
或 id
,因此我无法使用任何 DOM 操作选项,例如 .getElementById()
、.getElementByClassName
或 {{ 1}}。我需要使用 .getElementByTag()
将此 height
的 div
设置为 0
。
编辑
我很抱歉,我应该以更好的方式提出问题,并且我应该在提出问题之前提前预料并了解需求。这才是我真正需要的:
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
。
答案 0 :(得分:1)
您可以使用 document.getElementsByClassName('fht-cell')[0].children[0]
或类似的。
答案 1 :(得分:0)
first-child 选择器应该可以解决问题。 https://www.w3schools.com/cssref/sel_firstchild.asp