Javascript 如何引用多个index.innerHTML方法
如何为数组的多个索引指定.innerHTML? 我有一个数组Javascript 如何引用多个index.innerHTML方法,javascript,arrays,indexing,Javascript,Arrays,Indexing,如何为数组的多个索引指定.innerHTML? 我有一个数组 let poles = Array.prototype.slice.call(document.querySelectorAll(".pole")); 它包含9个空div,如下所示: <div class="pole" id="1"></div> poles[0][1][2].innerHTML ==='X' 或 与此相反: if(poles[0].innerHTML === 'X' &&
let poles = Array.prototype.slice.call(document.querySelectorAll(".pole"));
它包含9个空div,如下所示:
<div class="pole" id="1"></div>
poles[0][1][2].innerHTML ==='X'
或
与此相反:
if(poles[0].innerHTML === 'X' && poles[1].innerHTML === 'X' && poles[2].innerHTML === 'X')
我们可以做的是从数组中获取前3项,然后我们可以在这些项上使用,以查看每个项是否都有innerHTML
的X
,如下所示:
poles=Array.from(document.queryselectoral('div>span'))
设first3=poles.slice(0,3)
//我们也可以像这样手动构建阵列,这是需要的
//用于测试对角线匹配时
//设first3=[极点[0],极点[1],极点[2]]
if(first3.every(i=>i.innerHTML=='X')){
console.log('第1行包含所有x')
}否则{
console.log('第1行不包含所有x')
}
X
X
X
您可以使用以下功能:
var bol = true;
for (var x = 1, x<4, x++) {
if (poles[x].innerHTML != "X") {
bol = false;
}
}
if (bol) {
//There are only X
} else {
//There are not only X
}
var-bol=true;
对于(var x=1,xOh)来说,这是一个很好的解决方案,但是如果我有一行对角线,像这样呢?(poles[0]。innerHTML==player1&&poles[4]。innerHTML==player1&&poles[8]。innerHTML==player1&&poles[8]。innerHTML==player1)
,我不能像你一样分割它:/i我添加了一个测试玩家胜利的动态函数。看到我看到的最后一个代码片段,它是有效的,但是现在,我如何在8个胜利条件下做到这一点,我将用切片声明8个类似的变量?如果你能更详细地解释我,它如何在in、map和each中工作,我将非常高兴我不确定我还能解释什么。我在要点列表和代码中的注释中对其进行了最好的解释。如果你有什么不明白的地方,请指出。
var bol = true;
for (var x = 1, x<4, x++) {
if (poles[x].innerHTML != "X") {
bol = false;
}
}
if (bol) {
//There are only X
} else {
//There are not only X
}