gpt4 book ai didi

javascript - 如何同时更改多个id的内部html?

转载 作者:行者123 更新时间:2023-12-03 08:07:31 24 4
gpt4 key购买 nike

    var box1 = null;
var box2 = null;
var box3 = null;
var box4 = null;
var box5 = null;
var box6 = null;
var box7 = null;
var box8 = null;
var box9 = null;
var player = "X";

function clear() {
box1, box2, box3 , box4, box5, box6, box7, box8, box9 = null;
document.querySelectorAll("#1, #2, #3, #4, #5, #6, #7, #8, #9").innerHTML = " ";
}
function checkWin() {
if ((box1 == box2) && (box2 == box3) && (box1 != null)) {
alert(box1 + " wins.");
} else if ((box1 == box4) && (box4 == box7) && (box1 != null)) {
alert(box1 + " wins.");
} else if ((box1 == box5) && (box5 == box9) && (box1 != null)) {
alert(box1 + " wins.");
} else if ((box2 == box5) && (box5 == box8) && (box2 != null)) {
alert(box2 + " wins.");
} else if ((box3 == box6) && (box6 == box9) && (box3 != null)) {
alert(box3 + " wins.");
} else if ((box3 == box5) && (box5 == box7) && (box3 != null)) {
alert(box3 + " wins.");
} else if ((box4 == box5) && (box5 == box6) && (box4 != null)) {
alert(box4 + " wins.");
} else if ((box7 == box8) && (box8 == box9) && (box7 != null)) {
alert(box7 + " wins.");
} else {
}
}
function buttonClick(button) {
if (button == 1) {
document.getElementById("1").innerHTML = player;
if (player == "X") {
box1 = "X"
player = "O";
} else {
box1 = "O"
player = "X";
}
} else if (button == 2) {
document.getElementById("2").innerHTML = player;
if (player == "X") {
box2 = "X"
player = "O";
} else {
box2 = "O"
player = "X";
}
} else if (button == 3) {
document.getElementById("3").innerHTML = player;
if (player == "X") {
box3 = "X"
player = "O";
} else {
box3 = "O"
player = "X";
}
} else if (button == 4) {
document.getElementById("4").innerHTML = player;
if (player == "X") {
box4 = "X"
player = "O";
} else {
box4 = "O"
player = "X";
}
} else if (button == 5) {
document.getElementById("5").innerHTML = player;
if (player == "X") {
box5 = "X"
player = "O";
} else {
box5 = "O"
player = "X";
}
} else if (button == 6) {
document.getElementById("6").innerHTML = player;
if (player == "X") {
box6 = "X"
player = "O";
} else {
box6 = "O"
player = "X";
}
} else if (button == 7) {
document.getElementById("7").innerHTML = player;
if (player == "X") {
box7 = "X"
player = "O";
} else {
box7 = "O"
player = "X";
}
} else if (button == 8) {
document.getElementById("8").innerHTML = player;
if (player == "X") {
box8 = "X"
player = "O";
} else {
box8 = "O"
player = "X";
}
} else if (button == 9) {
document.getElementById("9").innerHTML = player;
if (player == "X") {
box9 = "X"
player = "O";
} else {
box9 = "O"
player = "X";
}
}
checkWin();
}
   

<body bgColor="black">
<center>
<button onClick="buttonClick(1)" id="1"></button>
<button onClick="buttonClick(2)" id="2"></button>
<button onClick="buttonClick(3)" id="3"></button>
<br>
<button onClick="buttonClick(4)" id="4"></button>
<button onClick="buttonClick(5)" id="5"></button>
<button onClick="buttonClick(6)" id="6"></button>
<br>
<button onClick="buttonClick(7)" id="7"></button>
<button onClick="buttonClick(8)" id="8"></button>
<button onClick="buttonClick(9)" id="9"></button>
</center>
</body>

在代码中,您可以看到我编写了一个“clear()”函数,该函数应该将所有变量设置回 null 并删除按钮的 innerHTML。我该如何通过 ID 更改所有按钮的 insideHTML?

最佳答案

您需要查看:

function clear(){
box1, box2, box3 ,box4, box5, box6, box7, box8, box9 = null;
[].forEach.call(document.querySelectorAll("#1, #2, #3, #4, #5, #6, #7, #8, #9"), function(el){
el.innerHTML = " ";
});
}

注意:

document.querySelectorAll("#1, #2, #3, #4, #5, #6, #7, #8, #9").forEach(function() {

});

这不起作用,因为你从 querySelectorAll 返回的不是一个数组,而是一个(非事件的)NodeList。

您也可以缩短此函数:

function buttonClick(button) {
document.getElementById(button).innerHTML = player;
if (player == "X") {
window["box"+button] = "X"
player = "O";
} else {
window["box"+button] = "O"
player = "X";
}

checkWin();

}

使用window[]访问全局变量。

关于javascript - 如何同时更改多个id的内部html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com