Home Web Front-end JS Tutorial Solution to the problem that IE browser does not support getElementsByClassName_javascript skills

Solution to the problem that IE browser does not support getElementsByClassName_javascript skills

May 16, 2016 pm 04:38 PM
ie browser Solution

The getElementsByClassName method has been added to DOM3, but it is not supported by other versions except IE9 and 10. This is a pain!
The current solution is to determine whether the browser supports this method. If it supports it, leave it alone. If it does not support it, add the getElementsByClassName method to the document object. This way of writing has the advantage that you don’t have to go there regardless of whether there is a native function or not. Modify the code.

Some people on the Internet directly define a getElementsByClassName function, but in this case, all uses of document.getElementsByClassName in the code need to be rewritten into getElementsByClassName. It's somewhat inconvenient and not universal.

The following method perfectly supports document writing:

if(!document.getElementsByClassName){
  document.getElementsByClassName = function(className, element){
    var children = (element || document).getElementsByTagName('*');
    var elements = new Array();
    for (var i=0; i<children.length; i++){
      var child = children[i];
      var classNames = child.className.split(' ');
      for (var j=0; j<classNames.length; j++){
        if (classNames[j] == className){ 
          elements.push(child);
          break;
        }
      }
    } 
    return elements;
  };
}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Why do I get ValueError: too many values ​​to unpack (expected 2) error when using pyecharts' Map? How to solve this problem? Why do I get ValueError: too many values ​​to unpack (expected 2) error when using pyecharts' Map? How to solve this problem? Apr 01, 2025 pm 07:42 PM

The reason and solution to the valueError:toomyvalueestounpack(expected2) error when using pyecharts' Map...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

The Python subprocess module fails to execute the wmic datafile command. How to solve it? The Python subprocess module fails to execute the wmic datafile command. How to solve it? Apr 01, 2025 pm 08:48 PM

Use Python's subprocess module to execute wmic...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to efficiently read Windows system logs and get only information from the last few days? How to efficiently read Windows system logs and get only information from the last few days? Apr 01, 2025 pm 11:21 PM

Efficient reading of Windows system logs: Reversely traverse Evtx files When using Python to process Windows system log files (.evtx), direct reading will be from the earliest...

How to solve the problem of wireless debugging failure in connecting to Xiaomi phones through adb using Mac? How to solve the problem of wireless debugging failure in connecting to Xiaomi phones through adb using Mac? Apr 01, 2025 pm 07:06 PM

Frequently Asked Questions about Wireless Debugging with Adb When using Mac for Android development, you often encounter the inability to use Adb...

See all articles