【发布时间】:2021-12-03 01:31:27
【问题描述】:
我正在尝试在答案块上创建一个 for 循环,我想让答案的高度为 30px 我尝试将事件添加到块中,但它不起作用我,那为什么?
这是我的解决方案:
HTML 代码:
<div class="blocks block-1">
<div class="questionContainer">
<div class="questions">How many team members can I invite?</div>
<div class="answers">
You can invite up to 2 additional users on the Free plan. There is
no limit on team members for the Premium plan.
</div>
</div>
</div>
<div class="blocks block-2">
<div class="questionContainer">
<div class="questions">What is the maximum file upload size?</div>
<div class="answers">
No more than 2GB. All files in your account must fit your allotted
storage space.
</div>
</div>
</div>
JavaScript 代码:
const block = document.getElementsByClassName(".blocks");
const answers = document.getElementsByClassName(".answers");
for (i = 0; i < block.length; i++) {
block[i].addEventListener("click", () => {
answers[i].style.height = "30px";
});
}
【问题讨论】:
-
@Terry 你错了!
toggle是原生事件 -> stackoverflow.com/questions/16751345/… -
如果您现在可以更好地理解,我编辑了这个问题..
-
+我尝试使用“点击”事件,但也没成功
-
哦,所以我也需要为它添加一个 for 循环吗?或做出类似的回答[i]?
-
@ADyson 在 Terry 发表评论时,PO 对元素的性质表示怀疑。此时很可能是
<details>。
标签: javascript for-loop dom dom-events