【问题标题】:IE 11: Object doesn't support property or method 'getElementsByClassName'IE 11:对象不支持属性或方法“getElementsByClassName”
【发布时间】:2019-05-22 21:33:53
【问题描述】:

这不是重复的。以前的问题与 IE8 有关。这发生在 IE11 中。

我在 Chrome 或 Firefox 中运行它没有问题,但我的代码必须在 IE11 中运行,并且我收到以下错误:

对象不支持属性或方法“getElementsByClassName”

function showNext(a) {
    var questions = document.getElementsByClassName("questionholder");
    showRequired.style.display = "none";

    for (var i = 0; i < questions.length; i++) {
        questions[i].style.display = "none";
    }

    var nextQuestion = document.getElementById("question" + a);

    if (nextQuestion !== null) {
        nextQuestion.style.display = "inline-block";
    }
}

代码应该看这里:

<form id="TheForm" style="display:block;">
    <div class="questionholder" id="question0" style="display:block">
        <a class="text2button" onclick="showNext(1)">Start</a>
    </div>
    <div class="questionholder" id="question1" style="display:block">
        <a class="text2button" onclick="showNext(2)">Q1</a>
    </div>
    <div class="questionholder" id="question2" style="display:block">
        <a class="text2button" onclick="showNext(3)">Q2</a>
    </div>
</form>

上面的代码会隐藏所有的 div,然后显示匹配 id "question"+a 的 div,由点击的按钮决定。

如何解决上述错误?

【问题讨论】:

  • 你能展示一个工作示例吗,因为 IE11 肯定有 document.getElementsByClassName
  • 不是重复的,这不是2014和IE8,IE9+都有这个方法。
  • 无法复制。我的 IE11 有document.getElementsByClassName
  • @JackBashford 这不是您链接到的问题的副本。这个问题是关于 IE8 的,其中 .getElementsByClassName() 没有实现。这个问题是关于IE11的,它在哪里。

标签: javascript internet-explorer-11 getelementsbyname


【解决方案1】:

修复方法如下:

<meta http-equiv="X-UA-Compatible" content="IE=11" />

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2012-03-23
    • 2019-01-28
    相关资源
    最近更新 更多