Javascript:按类名隐藏元素 [英] Javascript: Hide elements by class name

查看:104
本文介绍了Javascript:按类名隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html元素,我想从视图中隐藏,但我无法通过ID访问该元素,因为它没有ID,我无法为其分配ID。它有一个分配给它的类。有没有办法在没有id的情况下隐藏这个元素?

I have an html element that I want to hide from view, but I can't access that element by an ID because it has no ID and I can't assign an ID to it. It has a class assigned to it though. Is there any possible way to hide this element from view without having it's id?

推荐答案

getElementsByClassName 在某些浏览器中,但它没有像 getElementById 那样受到广泛支持。请注意,它会生成一个元素数组,而不仅仅是一个元素,因为多个元素可以具有相同的类。

There's getElementsByClassName in some browsers, but it's not as widely supported as getElementById. Note that it yields an array of elements, instead of just a single element, as several elements can have the same class.

如果您可以为父级分配ID,可能能够以其他方式访问它:

If you can assign an ID to a parent you might be able to access it in some other way:

document.getElementById('parent').getElementsByTagName('div')[3] // or whatever

这篇关于Javascript:按类名隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆