javascript document.getElementsByClassName compatibility with IE

前端 未结 7 567
逝去的感伤
逝去的感伤 2020-11-22 08:44

What is the best method to retrieve an array of elements that have a certain class?

I would use document.getElementsByClassName but IE does not support it.

相关标签:
7条回答
  • 2020-11-22 09:06
    function _getClass(whatEverClasNameYouWant){
    var a=document.getElementsByTagName('*');
       for(b in a){
          if((' '+a[b].className+' ').indexOf(' '+whatEverClasNameYouWant+' ')>-1){
          return a[b];
          }
       }
    }
    
    0 讨论(0)
  • 2020-11-22 09:07

    I just want to improve querySelectorAll fallback for IE8.

    Like others answered, the simple way is adding the function to Element.prototype with

    this.querySelectorAll('.' + className);
    

    But there are some problems:

    • It doesn't work with untrimmed strings (at the beginning).
    • It doesn't work with multiple classes.
    • It doesn't work with "strange" class characters (/, $, *, etc.)
    • It doesn't work with classes which begin with a digit (invalid identifiers)

    That means there should be some "fixing", for example:

    "abcd"     ->  ".abcd"
    "a   b cd" ->  ".a.b.cd"
    "   a b  " ->  ".a.b  "
    "a/b$c d"  ->  ".a\/b\$c.d"
    "1234"     ->  ".\000031234"
    

    Code:

    this.querySelectorAll(className
        .replace(/(?=[^ \w])/g, '\\')   // Escape non-word characters
        .replace(/\b\d/g, '\\00003$&')  // Escape digits at the beginning
        .replace(/(^| +)(?!$| )/g, '.') // Add "." before classes, removing spaces
    );
    
    0 讨论(0)
  • you may create the function for older browsers

    if (typeof document.getElementsByClassName!='function') {
        document.getElementsByClassName = function() {
            var elms = document.getElementsByTagName('*');
            var ei = new Array();
            for (i=0;i<elms.length;i++) {
                if (elms[i].getAttribute('class')) {
                    ecl = elms[i].getAttribute('class').split(' ');
                    for (j=0;j<ecl.length;j++) {
                        if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                            ei.push(elms[i]);
                        }
                    }
                } else if (elms[i].className) {
                    ecl = elms[i].className.split(' ');
                    for (j=0;j<ecl.length;j++) {
                        if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                            ei.push(elms[i]);
                        }
                    }
                }
            }
            return ei;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:09

    IE8:

    document.getElementsByClassName = function (className) {
        return document.querySelectorAll('.' + className)
    }
    
    0 讨论(0)
  • 2020-11-22 09:27

    It's not a method of document:

    function getElementsByClassName(node, classname) {
        var a = [];
        var re = new RegExp('(^| )'+classname+'( |$)');
        var els = node.getElementsByTagName("*");
        for(var i=0,j=els.length; i<j; i++)
            if(re.test(els[i].className))a.push(els[i]);
        return a;
    }
    
    tabs = getElementsByClassName(document.body,'tab');  // no document
    
    0 讨论(0)
  • 2020-11-22 09:32
    function getElementsByClassName(className) {
    if (document.getElementsByClassName) { 
      return document.getElementsByClassName(className); }
    else { return document.querySelectorAll('.' + className); } }
    

    Pretty sure this is the same as Leonid's function but this uses document.getElementsByClassName when it can.

    0 讨论(0)
提交回复
热议问题