テーブルを持っていて、セルに含まれるボタンがクリックされたときに各セルを繰り返し処理する必要があります。しかし、ループは実行されていないようで、私は問題を見つけることができません。ここforループがテーブルセルをループしないのはなぜですか? JavaScript
は、コードスニペットです:
element.onclick = function() {
//Sets color of selected element
for (var i = 0, cell; cell = document.getElementById('targetLocation').row.cells[i]; i++) {
if (cell.firstChild.style.background == "rgba(223, 22, 22, 0.53)") {
cell.firstChild.style.background = 'red';
}
else {
alert('Despite all, I loop')
};
};
};
<table id="targetLocation">
<tr>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
<th>J</th>
</tr>
<tr>
<th>1</th>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
<td><button class="cells" onmouseover="getTarget(this)">O</button></td>
</tr>
</table>
ここでは、関連するJavaScriptです:明確にするため
: "要素" は、あなたが上にマウスを移動している要素です。セル上をホバリングすると、背景色はrgba(223、22、22、0.53)に変わります。現在、そのセル上にマウスを置いてそのボタンをクリックすると、赤色に変わるはずです。 onclick関数自体は動作します。たとえば、その関数で警告を設定すると、それはうまくいきますので、forループが壊れていると思います。
あなたが働いてスニペットを投稿することができますか? – Nisarg
'行'とは何ですか? ['rows'](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/rows)のみです。 – PeterMader
@PeterMaderヒントありがとう! –