Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使函数正常工作-Javascript_Javascript_Html - Fatal编程技术网

无法使函数正常工作-Javascript

无法使函数正常工作-Javascript,javascript,html,Javascript,Html,使用class标记时,我无法使函数工作。尝试使用ID并正常工作,但我需要一个类。不起作用的是输入类=nextButton 以下是我的HTML代码的一部分: <div class="wrap"> <h3>Question2</h3> <p>Which National Team won the first World Cup?</p> &l

使用class标记时,我无法使函数工作。尝试使用ID并正常工作,但我需要一个类。不起作用的是输入类=nextButton

以下是我的HTML代码的一部分:

<div class="wrap">  
            <h3>Question2</h3>
                <p>Which National Team won the first World Cup?</p>
                    <div class="wraptext">
                        <input type="radio" name="question2" value="Brazil" /><strong>Brazil</strong><br />
                        <input type="radio" name="question2" value="France" /><strong>France</strong><br />
                        <input type="radio" name="question2" value="Italy" /><strong>Italy</strong><br />
                        <input type="radio" name="question2" value="Uruguay" /><strong>Uruguay</strong><br />
                        <input class="nextButton" type="button" value="Next Question">
                        <input class="prevButton" type="button" value="Previous Question">
                    </div>
        </div>
下面是Javascript:

var questions = document.getElementsByClassName("wrap");
var start = document.getElementById("startB");
var next = document.getElementsByClassName("nextButton");

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

start.addEventListener("click", showQuestions);

function showQuestions(){
  questions[0].style.display="";
}

next.addEventListener("click", nextQuestion);

function nextQuestion(){
  alert("hi");
}
使用此语法


var next=document.getElementsByClassName'nextButton'[0]

看起来您将GetElementsByCassName视为返回单个值而不是数组


循环遍历数组或添加[0]以仅引用它返回的第一个元素。

请将文本形式的代码添加到问题中。然后尝试打开控制台,点击F12。您将看到一个错误,它将为您的问题提供一个重要提示。尝试执行console.lognext.next不是HTMLElement,它是元素的集合,没有addEventListener方法