gpt4 book ai didi

javascript - 如何在 JavaScript 中隐藏某个类

转载 作者:行者123 更新时间:2023-11-30 15:01:26 25 4
gpt4 key购买 nike

我不知道为什么这不起作用。我试图隐藏某个类,这是我在 Javascript 上仅有的一行代码:

document.getElementsByClassName("popular").style.display = "none";

出于某种原因,我得到了错误:

"document" is not defined.

什么意思,不是变量。

请帮忙,谢谢。

最佳答案

getElementsByClassName 方法返回具有相同类的所有元素,将其视为具有相同类的元素数组。所以你需要指定,你想隐藏哪个元素。在我的示例中,我只有一个元素具有此类,因此我选择它就像选择数组的第一个元素 ([0])。您的代码应如下所示:

document.getElementsByClassName("popular")[0].style.display = "none";

console.log('Current "display" property value is: ' + document.getElementsByClassName("popular")[0].style.display)
<div class="popular">TARGET</div>

关于javascript - 如何在 JavaScript 中隐藏某个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46479728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com