How to solve the compatibility problem of native js method document.getElementsByClassName in ie8 and below
This article will explain in detail how to solve the compatibility problem of native js method document.getElementsByClassName in ie8 and below. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
The native js method "document.getElementsByClassName" cannot be used in ie8 and below browsers.
Modification: add compatibility judgment and change it to getClassNames method where you need to use this method.
The code is as follows:
Original method: document.getElementsByClassName ('tabs_div')
The calling method here is: getClassNames ('tabs_div',' div')
The code is as follows:
Var divs = getClassNames ('tabs_div',' div')
Function getClassNames (classStr,tagName) {
If (document.getElementsByClassName) {
Return document.getElementsByClassName (classStr)
} else {
Var nodes = document.getElementsByTagName (tagName), ret = []
For (I = 0; I < nodes.length; iTunes +) {
If (hasClass (Nodes [I], classStr)) {
Ret.push (Nodes [I])
}
}
Return ret
}
}
Function hasClass (tagStr,classStr) {
Var arr=tagStr.className.split (/\ swords /); / / this regular expression is because there can be multiple class to determine whether it contains
For (var iTuno Bandi)