arr = document.getElementsByClassName(type2); // suppose type2 is not available in the dom - class = "some_class"
// check for empty
此片段返回
[对象 HTMLCollection]
长度为0。
这是使用 getElementsByClassName 时检查类是否不存在的最佳方法,即仅检查长度是否为 0?
最佳答案
是的。检查返回集合的 length
属性。
由于 0
为假,您可以这样做:
var type2 = 'some_class';
var noElementHasType2Class = ! document.getElementsByClassName(type2).length;
关于javascript - 如何检查不存在的类? (按类名获取元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636500/