简体   繁体   English

getElementsByClassName()在IE6,IE7,IE8等旧的Internet Explorer中不起作用

[英]getElementsByClassName() doesn't work in old Internet Explorers like IE6, IE7, IE8

The following code: 以下代码:

var borderTds = document.getElementsByClassName('leftborder');

gives me an error message in Internet Explorer 6, 7 and 8: 在Internet Explorer 6,7和8中给出了一条错误消息:

Object does not support this method 对象不支持此方法

How can I select elements by their class in these browsers? 如何在这些浏览器中按类别选择元素?

I prefer not to use JQuery. 我不想使用JQuery。

IE6, Netscape 6+, Firefox, and Opera 7+ copy the following script in your page: IE6,Netscape 6 +,Firefox和Opera 7+在您的页面中复制以下脚本:

document.getElementsByClassName = function(cl) {
  var retnode = [];
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++) {
    if((' ' + elem[i].className + ' ').indexOf(' ' + cl + ' ') > -1) retnode.push(elem[i]);
  }
  return retnode;
}; 

This solution may help. 解决方案可能有所帮 This is a custom getElementsByClassName function implemented in pure javascript, that works in IE. 这是一个在纯javascript中实现的自定义getElementsByClassName函数,可以在IE中使用。

Essentially what this script is doing is probing, one by one, all possible options and picks the best one available. 基本上这个脚本正在做的是逐个探测所有可能的选项并选择最好的选项。 These options are: 这些选项是:

  1. Native document.getElementsByClassName function. Native document.getElementsByClassName函数。
  2. document.evaluate function, which allows evaluation of XPath queries. document.evaluate函数,允许评估XPath查询。
  3. Traversing the DOM tree. 遍历DOM树。

Of course the first one is the best performance-wise, however the latter should be available everywhere including IE 6. 当然第一个是性能最好的,但后者应该可以在任何地方使用,包括IE 6。

Usage example, which is also available on the page, looks like this: 用法示例(也在页面上可用)如下所示:

getElementsByClassName("col", "div", document.getElementById("container")); 

So the function allows 3 parameters: class (required), tag name (optional, searches for all tags if not specified), root element (optional, document if not specified). 因此该函数允许3个参数:class(必需),标记名称(可选,如果未指定则搜索所有标记),根元素(可选,文档,如果未指定)。

Update. 更新。 The solution linked in the blog post is hosted on the Google Code which is shutting down in Jan 2016. However the author has made it available on GitHub . 博客文章中链接的解决方案托管在2016年1月关闭的Google Code上。但是作者已将其发布在GitHub上 Kudos to flodin pointing this out in the comments. 感谢flodin在评论中指出这一点。

Internet Explorer 8 and older does not support getElementsByClassName() . Internet Explorer 8及更早版本不支持getElementsByClassName() If you only need a solution for IE8, it supports querySelectorAll() , you can use one of these instead. 如果您只需要IE8的解决方案,它支持querySelectorAll() ,您可以使用其中一个。 For older IEs you have to provide your own implementation, and for some other ancient browsers that support it you can also use evaluate() which runs XPath expressions. 对于较旧的IE,您必须提供自己的实现,对于其他支持它的古老浏览器,您还可以使用运行XPath表达式的evaluate()

This code provides a document.getElementsByClassName method if it does not exist yet using the methods I've described: 此代码提供了document.getElementsByClassName方法,如果它尚不存在,那么使用我所描述的方法:

if (!document.getElementsByClassName) {
  document.getElementsByClassName = function(search) {
    var d = document, elements, pattern, i, results = [];
    if (d.querySelectorAll) { // IE8
      return d.querySelectorAll("." + search);
    }
    if (d.evaluate) { // IE6, IE7
      pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
      elements = d.evaluate(pattern, d, null, 0, null);
      while ((i = elements.iterateNext())) {
        results.push(i);
      }
    } else {
      elements = d.getElementsByTagName("*");
      pattern = new RegExp("(^|\\s)" + search + "(\\s|$)");
      for (i = 0; i < elements.length; i++) {
        if ( pattern.test(elements[i].className) ) {
          results.push(elements[i]);
        }
      }
    }
    return results;
  }
}

If you don't like something about it, you can use your favorite search engine to find a different one. 如果您不喜欢它,可以使用自己喜欢的搜索引擎找到不同的搜索引擎。

The method doesn't exist in IE6. 该方法在IE6中不存在。 If you want to select elements by class and don't want to use a library, you simply have to loop through all elements in the page and check for the class in their className property. 如果要按类选择元素并且不想使用库,则只需遍历页面中的所有元素并在其className属性中检查类。

function getElementsByClassName(className) {
  var found = [];
  var elements = document.getElementsByTagName("*");
  for (var i = 0; i < elements.length; i++) {
    var names = elements[i].className.split(' ');
    for (var j = 0; j < names.length; j++) {
      if (names[j] == className) found.push(elements[i]);
    }
  }
  return found;
}

Demo: http://jsfiddle.net/kYdex/1/ 演示: http//jsfiddle.net/kYdex/1/

If getElementsByClassname does not support is error in some old browsers Simply try var modal = document.getElementById('myModal'); 如果getElementsByClassname不支持在某些旧浏览器中出错只需尝试var modal = document.getElementById('myModal'); modal.onclick= function(){ Then do what ever onclick function or another function by using getElementById modal.style.display = "none"; modal.onclick = function(){然后使用getElementById执行任何onclick函数或其他函数modal.style.display =“none”; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM