Internet Explorer 8 and earlier are not compatible with getElementsByClassName(). If you only need a solution for IE8, this one supports querySelectorAll(). For older versions of IE you need to provide your own implementation, and for some older browsers that support it you can also use evaluate()it to run XPath expressions.
This code provides a method document.getElementsByClassNameif it doesn't already exist using the methods kapa has described:
if (!document.getElementsByClassName) {
document.getElementsByClassName = function(search) {
var d = document, elements, pattern, i, results = [];
if (d.querySelectorAll) { // IE8
return d.querySelectorAll("." + search);
}
if (d.evaluate) { // IE6, IE7
pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
elements = d.evaluate(pattern, d, null, 0, null);
while ((i = elements.iterateNext())) {
results.push(i);
}
} else {
elements = d.getElementsByTagName("*");
pattern = new RegExp("(^|\\s)" + search + "(\\s|$)");
for (i = 0; i < elements.length; i++) {
if ( pattern.test(elements[i].className) ) {
results.push(elements[i]);
}
}
}
return results;
}
}
In the absence of time I google trasnlateo https://stackoverflow.com/a/25054465/1423096
Internet Explorer 8 and earlier are not compatible with
getElementsByClassName()
. If you only need a solution for IE8, this one supportsquerySelectorAll()
. For older versions of IE you need to provide your own implementation, and for some older browsers that support it you can also useevaluate()
it to run XPath expressions.This code provides a method
document.getElementsByClassName
if it doesn't already exist using the methods kapa has described:try this tag to make it compatible with EI