Javascript此关键字在addEventListener引用的函数中不起作用

Javascript此关键字在addEventListener引用的函数中不起作用,javascript,scope,this,addeventlistener,Javascript,Scope,This,Addeventlistener,我正在使用HTML和Javascript编写一个扫雷游戏,它允许用户输入网格有多大以及有多少地雷,当按下一个按钮时就会生成棋盘。该板由一个表组成,表中的行和单元格用Javascript生成,并带有几个循环。循环中有一个addEventListener语句,该语句用于在单击单元格时添加要调用的函数 我的问题是,我不知道如何将每个单独的单元格对象传递到函数中,以便我可以使用它。我一直在尝试使用 this.setAttribute(...) 但是我得到一个错误,它告诉我这个.setAttribute

我正在使用HTML和Javascript编写一个扫雷游戏,它允许用户输入网格有多大以及有多少地雷,当按下一个按钮时就会生成棋盘。该板由一个表组成,表中的行和单元格用Javascript生成,并带有几个循环。循环中有一个addEventListener语句,该语句用于在单击单元格时添加要调用的函数

我的问题是,我不知道如何将每个单独的单元格对象传递到函数中,以便我可以使用它。我一直在尝试使用

this.setAttribute(...)
但是我得到一个错误,它告诉我这个.setAttribute不是一个函数。我知道“这”可能指的是全球范围,但我不确定如何解决我的问题,我在其他地方也很难找到解决方案。以下是相关代码:

function createGame(){
    size= document.getElementById("boardSize").value;
    //other stuff
    var table= document.createElement("table");
    table.id= "gameBoard";
    document.body.appendChild(table);
    for(var r= 1; r <= size; r++){
        var row= document.createElement("tr");
        row.setAttribute("id", "r" + r.toString(10));
        document.getElementById("gameBoard").appendChild(row);
        for(var c= 1; c <= size; c++){
            var cell= document.createElement("td");
            cell.setAttribute("id", "r" + r.toString(10) + "c" + c.toString(10));
            cell.setAttribute("class", "uClear");
            cell.innerHTML= "?";                                           
            document.getElementById("r" + r.toString(10)).appendChild(cell);
            cell.addEventListener("click", clickClear);
        }
    }
    //other stuff
}
//the following is in global scope
function clickClear(){
    this.setAttribute("class", "kClear");   //this is undefined
    //other stuff
}
函数createGame(){
大小=document.getElementById(“boardSize”).value;
//其他东西
var table=document.createElement(“表”);
table.id=“游戏板”;
document.body.appendChild(表);

对于(var r=1;r我将这样做,通过在变量内传递元素引用:

    size= document.getElementById("boardSize").value;
    //other stuff
    var table= document.createElement("table");
    table.id= "gameBoard";
    document.body.appendChild(table);
    for(var r= 1; r <= size; r++){
        var row= document.createElement("tr");
        row.setAttribute("id", "r" + r.toString(10));
        document.getElementById("gameBoard").appendChild(row);
        for(var c= 1; c <= size; c++){
            var cell= document.createElement("td");
            cell.setAttribute("id", "r" + r.toString(10) + "c" + c.toString(10));
            cell.setAttribute("class", "uClear");
            cell.innerHTML= "?";                                           
            document.getElementById("r" + r.toString(10)).appendChild(cell);
            cell.addEventListener("click",() => clickClear(cell)); // arrow function
        }
    }
    //other stuff
}
//the following is in global scope
function clickClear(element){
    element.setAttribute("class", "kClear");   // pass the element reference instead
    //other stuff
}
size=document.getElementById(“boardSize”).value;
//其他东西
var table=document.createElement(“表”);
table.id=“游戏板”;
document.body.appendChild(表);

对于(var r=1;r请发布a。您可以使用a使其可执行。它对我来说很好:EventListener的
this
上下文(不在箭头函数中)是元素,它将是
单元格
或在循环的每个步骤中创建的
元素,因此这不是问题所在。调用函数时,可能元素没有正确加载?嗯。这并不能解决问题,尽管我确信它应该工作。我开始认为我的o在早些时候有问题组织或等待某物装载