简体   繁体   English

向 JavaScript 中的按钮添加 onclick 函数

[英]Adding onclick functions to buttons within JavaScript

I'm trying to make a quiz in JavaScript.我正在尝试使用 JavaScript 进行测验。 Basically my code has a button that starts the quiz, and when I click it, I remove the button and add four more.基本上我的代码有一个开始测验的按钮,当我点击它时,我删除了该按钮并添加了四个按钮。 To each of the buttons I added an onclick attribute.我为每个按钮添加了一个 onclick 属性。

responseOne.onclick = changeScore();

I expect this line to run the changeScore function when I click on the 'responseOne' button.当我单击“responseOne”按钮时,我希望这一行运行changeScore函数。 The problem is when I run the function startQuiz() , the function changeScore runs four times before even removing and adding any of the buttons.问题是当我运行函数startQuiz() ,函数changeScore在删除和添加任何按钮之前运行了四次。 What's wrong with my code?我的代码有什么问题?

Also I'm really new so if any of my other code is inefficient or something, constructive criticism is always welcome too :)另外我真的很新,所以如果我的任何其他代码效率低下或其他什么东西,也总是欢迎建设性的批评:)

function startQuiz() {

//remove the start quiz button
    var element = document.getElementById("dummy")
    element.parentNode.removeChild(element);
//change the h1 and h2 html tags to the question number and question
    h1.innerHTML = title[questionNumber];
    h2.innerHTML = questions[questionNumber];
//create buttons and give them bootstrap and onclick functions, appending them to empty divs
    var responseOne = document.createElement("button");
    responseOne.innerHTML = responseA[questionNumber];
    responseOne.onclick = changeScore();
    responseOne.classList.add("btn");
    responseOne.classList.add("btn-primary");
    document.getElementById("button1").appendChild(responseOne);

    var responseTwo = document.createElement("button");
    responseTwo.innerHTML = responseB[questionNumber];
    responseTwo.onclick = changeScore();
    responseTwo.classList.add("btn");
    responseTwo.classList.add("btn-danger");
    document.getElementById("button2").appendChild(responseTwo);

    var responseThree = document.createElement("button");
    responseThree.innerHTML = responseC[questionNumber];
    responseThree.onclick = changeScore();
    responseThree.classList.add("btn");
    responseThree.classList.add("btn-warning");
    document.getElementById("button3").appendChild(responseThree);

    var responseFour = document.createElement("button");
    responseFour.innerHTML = responseD[questionNumber];
    responseFour.onclick = changeScore();
    responseFour.classList.add("btn");
    responseFour.classList.add("btn-success");
    document.getElementById("button4").appendChild(responseFour);
}

function changeScore() {
    alert("changeScore function is running");
}

although there pretty good ways to do that in JavaScript.尽管在 JavaScript 中有很好的方法可以做到这一点。 But, in this case automatic running changeScore() can be handled this way.但是,在这种情况下,可以通过这种方式处理自动运行的changeScore()

responseOne.onclick = changeScore();

replace above line with this用这个替换上面的行

responseOne.onclick = changeScore;

do it for all responseOne,responseTwo, responseThree, and responseFour.对所有 responseOne、responseTwo、responseThree 和 responseFour 执行此操作。

😃 😃

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

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