Javascript onclick在forloop范围内出现问题

Javascript onclick在forloop范围内出现问题,javascript,Javascript,我正在尝试创建onclick事件,该事件将更改div的颜色,到目前为止,这是我所做的: for (var x = 0; x<question_details.length; x++){ var question_div_id = document.getElementsByName(question_details[x].question); answer = question_details[x].answer; divs = question_div_id;

我正在尝试创建onclick事件,该事件将更改div的颜色,到目前为止,这是我所做的:

for (var x = 0; x<question_details.length; x++){
    var question_div_id = document.getElementsByName(question_details[x].question);
    answer = question_details[x].answer;
    divs = question_div_id;
    changeColor(answer, divs);
}

function changeColor(answer, divs) {
    for (var y = 0; y<divs.length; y++){
        if (divs[y].id == answer){
            document.getElementById(divs[y].id).onclick = function (num) {
                document.getElementById(divs[num].id).style.backgroundColor="green";
            }(y);
        }else{
            document.getElementById(divs[y].id).onclick = function(num) {
                document.getElementById(divs[num].id).style.backgroundColor="red";
            }(y);
        }
    }
}

for(var x=0;x您的函数changeColor应该只检查答案是否正确,然后更改div的颜色

在第一个for循环中,您需要向每个包含答案的div添加一个。这样,每次单击都可以检查答案

应该是这样的:

question_div_id.addEventListener("click", function(){
    answer = question_details[x].answer;
    divs = question_div_id;
    changeColor(answer, divs);
}); 

您的函数changeColor应该只检查答案是否正确,然后更改div的颜色

在第一个for循环中,您需要向每个包含答案的div添加一个。这样,每次单击都可以检查答案

应该是这样的:

question_div_id.addEventListener("click", function(){
    answer = question_details[x].answer;
    divs = question_div_id;
    changeColor(answer, divs);
}); 

您正在执行函数,并将Undefined分配给onclick,以便传递函数:

function changeColor(answer, divs) {
    for (var y = 0; y<divs.length; y++){
        if (divs[y].id == answer){
            document.getElementById(divs[y].id).onclick = function (num) {
                return function() {
                    document.getElementById(divs[num].id)
                        .style.backgroundColor="green";
                };
            }(y);
        }else{
            document.getElementById(divs[y].id).onclick = function(num) {
                return function() {
                    document.getElementById(divs[num].id)
                        .style.backgroundColor="red";
                };
            }(y);
        }
    }
}
函数更改颜色(答案,divs){

对于(var y=0;y您正在执行函数并将Undefined分配给onclick,您需要传递一个函数:

function changeColor(answer, divs) {
    for (var y = 0; y<divs.length; y++){
        if (divs[y].id == answer){
            document.getElementById(divs[y].id).onclick = function (num) {
                return function() {
                    document.getElementById(divs[num].id)
                        .style.backgroundColor="green";
                };
            }(y);
        }else{
            document.getElementById(divs[y].id).onclick = function(num) {
                return function() {
                    document.getElementById(divs[num].id)
                        .style.backgroundColor="red";
                };
            }(y);
        }
    }
}
函数更改颜色(答案,divs){

对于(var y=0;y),请查看事件处理程序的第一个参数是无法向其传递参数的事件对象。请查看事件处理程序的第一个参数是无法向其传递参数的事件对象。