我有一段代码,当你点击表格单元格时,它会将颜色切换为红色,并关闭该行中已经变为红色的任何单元格,以便在一行中只有一个单元格是红色的。
这也发生在第二行,以便该行中只有一个单元格是红色的。
我想要的是整个表格中只有一个单元格是红色的,这将是一个13行的表格,只有一个单元格永远是红色的。非常感谢您的帮助。
var el
function togCell(col){
if (typeof event!=='undefined')
el=event.srcElement
for (var i = 0; i < el.parentNode.cells.length; i++)
el.parentNode.cells[i].style.backgroundColor=''
el.style.backgroundColor=col
}
if (window.addEventListener)
window.addEventListener('click', function(e){el=e.target}, true)
table {
cursor:text;
}
td {
font-size:14;
cursor:default;
}
<table border="1" cellpadding="8" cellspacing="2">
<tr style="background-color:white;">
<td onclick="togCell('red')">AA</td>
<td onclick="togCell('red')">AKs</td>
<td onclick="togCell('red')">AQs</td>
<td onclick="togCell('red')">AJs</td>
<td onclick="togCell('red')">ATs</td>
<td onclick="togCell('red')">A9s</td>
<td onclick="togCell('red')">A8s</td>
<td onclick="togCell('red')">A7s</td>
<td onclick="togCell('red')">A6s</td>
<td onclick="togCell('red')">A5s</td>
<td onclick="togCell('red')">A4s</td>
<td onclick="togCell('red')">A3s</td>
<td onclick="togCell('red')">A2s</td>
</tr>
<tr style="background-color:white;">
<td onclick="togCell('red')">AKo</td>
<td onclick="togCell('red')">KK</td>
<td onclick="togCell('red')">KQs</td>
<td onclick="togCell('red')">KJs</td>
<td onclick="togCell('red')">KTs</td>
<td onclick="togCell('red')">K9s</td>
<td onclick="togCell('red')">K8S</td>
<td onclick="togCell('red')">K7s</td>
<td onclick="togCell('red')">K6s</td>
<td onclick="togCell('red')">K5s</td>
<td onclick="togCell('red')">K4s</td>
<td onclick="togCell('red')">K3s</td>
<td onclick="togCell('red')">K2s</td>
</tr>
</table>