Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the compatibility problem of native js method document.getElementsByClassName in ie8 and below

Shulou Source: shulou.com Published: 2022-06-03 07:51:54 11月15日 Update

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)

Tags: Methods compatibility problems code content articles more knowledge articles good location multiple regular browser expression quality reference help related browse Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Linux Redmi OPPO Reno