Internet Explorer does not support getElementsByClassName. getelementsbyclass

Source: Internet
Author: User

Internet Explorer does not support getElementsByClassName. getelementsbyclass

The getElementsByClassName method has been added to DOM3. However, other versions except IE9 and 10 do not support this method. This is a pain point!
Currently, this solution can be used to determine that the browser does not support this method. If it does not support this method, add the getElementsByClassName Method to the document Object. This method has the following advantages, no matter whether there are native functions, you do not need to modify the code.

Some people on the Internet directly define a getElementsByClassName function, but in this case, all the use of document. getElementsByClassName in the Code must be rewritten to getElementsByClassName. It is inconvenient and not common.

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;  };}

The IE browser does not support var = documentgetElementsByClassName ('');

IE6, 7,8 does not seem to have this function.
If you want to use jquery, you can simply use $ (". Style name,

Why does IE not support getElementsByClassName ()? I won't use JS Code. The simple JS Code gets the class value.

GetElementsByClassName () has never been written in this way.
GetElementsById (op). className to get the css class of the current object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.