纯Javascript-函数生成的onclick元素的变化class
Pure Javascript- Change class of onclick element generated by function
我是编码方面的新手,几天来一直在解决一个问题,但没有找到解决方案。
我正在尝试使用由正方形组成的网格构建一个 canvas,当单击正方形时,它会更改其背景颜色。
我正在学习纯 javascript,我的问题是我无法找到一种方法来引用单击的元素并仅更改其 class,因为我的网格是由函数生成的。我这样做是因为我希望将来能够更改 canvas 大小。
关于如何进行的任何建议?
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Square Canvas Game</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap" rel="stylesheet">
</head>
<body onload="javascript:setInitialCanvas()">
<header>
<h1>Square artist</h1>
</header>
<div id="gameInterfaceContainer">
<button id="clearCanvas" onclick="clearAllSquares()">Clear All</button>
<div id="canvas">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
JS
function setInitialCanvas(){
for (let i =0; i<400; i++){
let initialSquare = document.createElement("div");
document.getElementById("canvas").appendChild(initialSquare);
initialSquare.className = "canvasSquare clearSquare";
initialSquare.onclick = changeSquareColor(this);
initialSquare.style.backgroundColor = "white";
initialSquare.id = "square" + i;
}
}
function changeSquareColor(this){
let squareColor = ["white","blue","black","green","red","yellow","gray","brown"];
let colorIndex = 0;
while (squareColor[colorIndex] != this.style.backgroundColor){
colorIndex += 1;
}
if(this.style.backgroundColor == squareColor[squareColor.length]){
this.style.backgroundColor = squareColor[0];
}else
colorIndex += 1;
this.style.backgroundColor = squareColor[colorIndex];
}
谢谢!
*编辑:添加代码笔(不起作用)https://codepen.io/ccue92/pen/qBNKLQY
它不起作用的原因是您没有将 changeSquareColor 函数绑定到 onclick 事件,而是绑定了它的结果。
您需要像这样更改 onclick 绑定:
initialSquare.onclick = function(event) {
changeSquareColor(event.target);
}
并且不要使用“this”作为变量名,因为它是当前作用域的保留字:
function changeSquareColor(square) {
let squareColor = ["white","blue","black","green","red","yellow","gray","brown"];
let colorIndex = 0;
while (squareColor[colorIndex] != square.style.backgroundColor){
colorIndex += 1;
}
if(square.style.backgroundColor == squareColor[squareColor.length]){
square.style.backgroundColor = squareColor[0];
} else {
colorIndex += 1;
}
square.style.backgroundColor = squareColor[colorIndex];
}
我是编码方面的新手,几天来一直在解决一个问题,但没有找到解决方案。 我正在尝试使用由正方形组成的网格构建一个 canvas,当单击正方形时,它会更改其背景颜色。 我正在学习纯 javascript,我的问题是我无法找到一种方法来引用单击的元素并仅更改其 class,因为我的网格是由函数生成的。我这样做是因为我希望将来能够更改 canvas 大小。 关于如何进行的任何建议?
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Square Canvas Game</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap" rel="stylesheet">
</head>
<body onload="javascript:setInitialCanvas()">
<header>
<h1>Square artist</h1>
</header>
<div id="gameInterfaceContainer">
<button id="clearCanvas" onclick="clearAllSquares()">Clear All</button>
<div id="canvas">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
JS
function setInitialCanvas(){
for (let i =0; i<400; i++){
let initialSquare = document.createElement("div");
document.getElementById("canvas").appendChild(initialSquare);
initialSquare.className = "canvasSquare clearSquare";
initialSquare.onclick = changeSquareColor(this);
initialSquare.style.backgroundColor = "white";
initialSquare.id = "square" + i;
}
}
function changeSquareColor(this){
let squareColor = ["white","blue","black","green","red","yellow","gray","brown"];
let colorIndex = 0;
while (squareColor[colorIndex] != this.style.backgroundColor){
colorIndex += 1;
}
if(this.style.backgroundColor == squareColor[squareColor.length]){
this.style.backgroundColor = squareColor[0];
}else
colorIndex += 1;
this.style.backgroundColor = squareColor[colorIndex];
}
谢谢!
*编辑:添加代码笔(不起作用)https://codepen.io/ccue92/pen/qBNKLQY
它不起作用的原因是您没有将 changeSquareColor 函数绑定到 onclick 事件,而是绑定了它的结果。
您需要像这样更改 onclick 绑定:
initialSquare.onclick = function(event) {
changeSquareColor(event.target);
}
并且不要使用“this”作为变量名,因为它是当前作用域的保留字:
function changeSquareColor(square) {
let squareColor = ["white","blue","black","green","red","yellow","gray","brown"];
let colorIndex = 0;
while (squareColor[colorIndex] != square.style.backgroundColor){
colorIndex += 1;
}
if(square.style.backgroundColor == squareColor[squareColor.length]){
square.style.backgroundColor = squareColor[0];
} else {
colorIndex += 1;
}
square.style.backgroundColor = squareColor[colorIndex];
}