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

时间:2019-05-22 21:33:54

标签: javascript internet-explorer-11 getelementsbyname

这不是重复项。先前的问题与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。

如何解决以上错误?

2 个答案:

答案 0 :(得分:0)

修复如下:

File.WriteAllText(filepath, decoded);

答案 1 :(得分:-2)

  

如何解决以上错误?

Never ever use .getElementsByClassName() again,而改用.querySelectorAll()

function showNext(a) {
    var questions = document.querySelectorAll(".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";
    }
}

showNext(2);
<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>