javascript 在javascript中按类获取元素并计算其中元素的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11950253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:51:39  来源:igfitidea点击:

Get element by class in javascript and count the number of elements in them

javascript

提问by Kaushik Balasubramanain

Possible Duplicate:
How to Get Element By Class in JavaScript?

可能的重复:
如何在 JavaScript 中按类获取元素?

I have a div tag with a class name on it. I want to get that div tag using the class name and count the number of elements in them. I am not in a position to filter using ID. How do i achieve this?

我有一个带有类名的 div 标签。我想使用类名获取该 div 标签并计算其中的元素数量。我无法使用 ID 进行过滤。我如何实现这一目标?

回答by Shreedhar

Had you googled it.

如果你用谷歌搜索它。

var arr = document.getElementsByClassName("classname")

arr.length