这可能是一个简单的问题,但有没有办法把它写成循环?
function playOne() {
if (playOne) {
document.getElementById("squareOne").innerHTML = '<img src="img/cross.png">';
}
}
function playTwo() {
if (playTwo) {
document.getElementById("squareTwo").innerHTML = '<img src="img/cross.png">';
}
}
function playThree() {
if (playThree) {
document.getElementById("squareThree").innerHTML = '<img src="img/cross.png">';
}
}
答案 0 :(得分:0)
你的意思是?
let elems = {
"squareOne": playOne,
"squareTwo": playTwo,
"squareThree": playThree,
}
for (var key in elems) {
if(elems[key]) {
document.getElementById(key).innerHTML = '<img src="img/cross.png">';
}
}