javascript - getElementsByClassName() doesn't work in old Internet Explorers like IE6, IE7, IE8 - Stack Overflow

The following code:var borderTds = document.getElementsByClassName('leftborder');gives me an

The following code:

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

gives me an error message in Internet Explorer 6, 7 and 8:

Object does not support this method

How can I select elements by their class in these browsers?

I prefer not to use JQuery.

The following code:

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

gives me an error message in Internet Explorer 6, 7 and 8:

Object does not support this method

How can I select elements by their class in these browsers?

I prefer not to use JQuery.

Share Improve this question edited Jul 31, 2014 at 9:10 kapa 78.7k21 gold badges164 silver badges177 bronze badges asked Jul 5, 2011 at 14:49 124697124697 21.9k69 gold badges197 silver badges319 bronze badges 4
  • possible duplicate of getElementsByClassName & IE – Felix Kling Commented Jul 5, 2011 at 14:52
  • 1 I think jQuery supports such functionality. – ChaosPandion Commented Jul 5, 2011 at 14:52
  • 1 An alternative to using jQuery would be to just use the Sizzle selector engine. But if all you need is to select by class, then I'd just write a replacement. – user113716 Commented Jul 5, 2011 at 14:55
  • 2 @ChaosPandion: "I prefer not to use JQuery". – Felix Kling Commented Jul 5, 2011 at 14:55
Add a comment  | 

5 Answers 5

Reset to default 16

IE6, Netscape 6+, Firefox, and Opera 7+ copy the following script in your page:

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.

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.
  2. document.evaluate function, which allows evaluation of XPath queries.
  3. Traversing the DOM tree.

Of course the first one is the best performance-wise, however the latter should be available everywhere including 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).

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. Kudos to flodin pointing this out in the comments.

Internet Explorer 8 and older does not support getElementsByClassName(). If you only need a solution for IE8, it supports querySelectorAll(), you can use one of these instead. 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.

This code provides a document.getElementsByClassName method if it does not exist yet using the methods I've described:

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. 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.

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/

If getElementsByClassname does not support is error in some old browsers Simply try var modal = document.getElementById('myModal'); modal.onclick= function(){ Then do what ever onclick function or another function by using getElementById modal.style.display = "none"; }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737519297a3953893.html

相关推荐

  • win10局域网加入工作组计算机,Win10专业版加入局域网工作组的技巧

    在电脑系统中的很多功能组件都需要工作组的帮助&#xff0c;如果工作组不一样&#xff0c;很有可能无法给予用户连接环境&#xff0c;如果用户计算机上的工作组和别的计算机上不一样&#xff0c;不过即使电脑

    10小时前
    00
  • ubuntu的操作系统桌面版

    下载Ubuntu桌面版访问Ubuntu官网:Download Ubuntu Desktop | Ubuntu选择最新LTS版本(推荐)或最新标准版本点击"Download"按钮获取ISO镜像文件创建启动盘准备一个至少4

    9小时前
    00
  • 【Linux】用C++实现UDP通信:详解socket编程流程

    协议(Protocol)协议 是计算机或通信系统中,不同实体(如设备、程序、服务等)之间进行交互和通信时,共同遵循的一套规则和标准。它定义了数据的格式、传输方式、错误处理、安全机制等,确保通信双方能够正确理解彼此的信息并完成协作。协议的核心

    9小时前
    00
  • 替换WIN键和Ctrl键

    用惯了Mac&#xff0c;最近切换到了win系统的电脑&#xff0c;老是习惯按win键&#xff0c;所以就想着把系统的win键和ctrl键对调。然后网上就找到可如下方法&#xff1a; 按下winr&

    9小时前
    00
  • YOLO半自动标注技术助力铁路检测,人工标注时间骤降80%!

    论文题目:A YOLO-Based Semi-Automated Labeling Approach to Improve Fault Detection Efficiency in Railroad Videos论文链接:.0101

    9小时前
    00
  • 【位运算】判定字符是否唯一 &amp;&amp; 丢失的数字

    面试题 01.01. 判定字符是否唯一面试题 01.01. 判定字符是否唯一​ 实现一个算法,确定一个字符串 s 的所有字符是否全都不同。示例 1:代码语言:javascript代码运行次数:0运行复制输入: s = "leetc

    9小时前
    00
  • 【进程信号】三、阻塞信号

    Ⅰ. 信号相关常见概念实际执行信号的处理动作称为信号递达(Delivery)。信号从产生到递达之间的状态,称为信号未决(Pending)。进程可以选择阻塞 (Block)某个信号。被阻塞的信号产生时将保持在未决状态,直到进程解除对此信号的阻

    9小时前
    00
  • .NET 平台上的开源模型训练与推理进展

    .NET 平台上的开源模型训练与推理进展作者:痴者工良博客:电子书仓库:Maomi.Torch 项目仓库:.Torch一、 .NET AI 生态概述背景介绍.NET 生态系统已经成为支持多种编程语言、多种平台和大量开发工具的强大生态系统。最

    9小时前
    00
  • DeepSeek 云端训练全流程实录:TI One 新手也能轻松上手

    摘要想快速上手大模型训练,但又对平台操作一头雾水?这篇文章将带你从 0 开始配置、训练并微调 DeepSeek 模型,全程基于腾讯云 TI One 平台进行实操。不但有详细的步骤讲解,还有实用代码 Demo 帮你跑通训练链路,让云端训练变得

    8小时前
    10
  • 蓝桥杯救命系列(省三必会,暴力解决)

    1.题目概述这个题目其实我看了好几天了,这个是苯环哥哥出的一个题目,在洛谷这个网站上,这个网站我一直都有听说过,但是自己也是第一次使用,在这个代码的编译相关的过程上面确实是遇到了很多的问题,现在还没有完全解决,但是我觉得这个时候有点收获,想

    8小时前
    00
  • WordPress外贸独立站被挂码的原因

    WordPress外贸独立站被挂码的原因主要有以下几点:网站自身漏洞WordPress核心漏洞:WordPress的代码可能存在漏洞,黑客会利用这些漏洞入侵网站。例如,早期版本的WordPress存在SQL注入漏洞,攻击者可以通过构造恶意的

    8小时前
    10
  • 你知道吗?网站没有安装SSL证书,会有这三个严重影响!

    当前钓鱼欺诈网站泛滥,黑客攻击事件频发,这些都会对网站数据安全构成严重威胁。因此,除了银行、电商类网站安装SSL证书之外,如今企业网站安装SSL证书的也越来越多。网站不使用SSL证书,企业会有什么损失?今天就来讲讲这个影响损失。严重影响一

    8小时前
    00
  • 备份和恢复Exchange 2010的SSL证书

    一、备份Exchange 2010 SSL证书(一)使用Exchange管理控制台(EMC)备份证书1.打开Exchange管理控制台以具有管理员权限的用户身份登录到安装有Exchange 2010的服务器。点击“开始”菜单,在“程序”中找

    8小时前
    20
  • 生成式人工智能的价值回归:重塑技术、社会与个体的发展轨迹

    在数字化浪潮的席卷之下,生成式人工智能(Generative AI)正以前所未有的速度重塑人类社会的面貌。这项技术不仅被视为人工智能发展的新阶段,更被赋予了推动生产力跃升、加速社会形态变革的历史使命。生成式人工智能的价值回归,不仅体现在技术

    8小时前
    10
  • 【Golang】企业内网专网离线搭建Go环境实战指南

    背景说明随着数据泄露问题日益严重和大模型的快速发展,越来越多的公司将研发环境迁移至内网甚至专网。而由于网络限制或安全策略,直接访问外部资源(如Go模块仓库)可能会受到限制。这就需要我们在公司内网中搭建一个离线的Go语言开发环境,以确保能够顺

    8小时前
    00
  • 鸿蒙版 Flutter 3.22.0

    鸿蒙版 Flutter 3.22.0-ohos-0.1.1 发布近日在更新仓库地址地址的时候 Flutter 3.22.0-ohos-0.1.1 发布了。版本概述本版本为基于 Flutter 3.22.0 适配的 HarmonyOS 版本。

    8小时前
    00
  • docker搭建zabbix监控

    官网部署文档创建统一的网络代码语言:bash复制docker network create --subnet 172.20.0.016 --ip-range 172.20.240.020 zabbix-net搭建mysql数据库代码语言

    8小时前
    00
  • R语言《红楼梦》文本挖掘:词频统计、词云可视化及前后对比分析

    全文链接:?p=34319作为中国古典文学的瑰宝,《红楼梦》具有极高的文学价值和丰富的主题内涵。近年来,随着大数据和文本挖掘技术的发展,对《红楼梦》等古典文学作品的深度分析成为可能。本研究采用R语言作为分析工具,对《红楼梦》全文进行文本挖

    8小时前
    00
  • 戴尔笔记本恢复原装系统全攻略

    戴尔笔记本恢复原装系统全攻略 戴尔笔记本电脑作为市场上的热门品牌,凭借其出色的性能和稳定的运行赢得了广大用户的喜爱。然而,在长期的使用过程中,系统可能会因为各种原因出现问题,如病毒感染、软件冲突、系统文件损坏等。这些问题不仅会影响电脑的正

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信