IE browsers do not support Getelementsbyclassname solutions _javascript Tips

Source: Internet
Author: User

In the DOM3 has joined the Getelementsbyclassname this method, however IE9, 10 other versions are not supported, this is a piece of pain!
This can be resolved so far, that the browser does not support this method, if the support does not matter, if not supported, in the Document object to add Getelementsbyclassname this method, such a writing has the advantage that no matter whether there is no native function you do not have to modify the code.

Some people on the Web directly define a getelementsbyclassname function, but in this case, all the use of document.getelementsbyclassname in the code should be rewritten as getelementsbyclassname. It's a bit 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;}

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.