作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道为什么这不起作用。我试图隐藏某个类,这是我在 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/
我是一名优秀的程序员,十分优秀!