arr = document.getElementsByClassName(type2); // suppose type2 is not available in the dom - class = "some_class"
// check for empty
This snippet returns
[object HTMLCollection]
which has a length of 0.
Is this the best way to check for the class not existing when getElementsByClassName is used, ie, just check for a length of 0?
Yes. Check the length
property of the returned collection.
Since 0
is falsy, you can do this:
var type2 = 'some_class';
var noElementHasType2Class = ! document.getElementsByClassName(type2).length;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.