[英]Changing background-color of td on checked
How do I do this? 我该怎么做呢? I have a
checkbox
that when you click/check the full td
column will be having a background color. 我有一个
checkbox
,当您单击/选中完整的td
列时,它将具有背景色。 Then if it is unchecked, it will remove its background-color. 然后,如果未选中,它将删除其背景色。 Also if it is checked already and the user checked another
checkbox
, the one checked will have the background-color and the previous one will have no background color. 同样,如果已经选中并且用户选中了另一个
checkbox
,则选中的复选框将具有背景色,而前一个checkbox
将没有背景色。
You should just add a col tags to your HTML table. 您应该只将col标签添加到HTML表中。 You can then set the background-color of the col and will make all the TDs in the column that color:
然后,您可以设置col的背景色,并使列中的所有TD都变为彩色:
function ChangeColColor(chkCol,col) { var varCol = document.getElementById(col); var varColor = "White"; if (chkCol.checked == true) { varColor = "Red"; } varCol.style.backgroundColor = varColor; }
<table> <colgroup> <col id="colA"> <col id="colB"> <col id="colC"> </colgroup> <tr> <td>Column A <input id="chkColA" type="checkbox" onclick="ChangeColColor(this,'colA')"/></td> <td>Column B <input id="chkColB" type="checkbox" onclick="ChangeColColor(this,'colB')"/></td> <td>Column C <input id="chkColC" type="checkbox" onclick="ChangeColColor(this,'colC')"/></td> </tr> <tr> <td>Data A:1</td> <td>Data B:1</td> <td>Data C:1</td> </tr> <tr> <td>Data A:2</td> <td>Data B:2</td> <td>Data C:2</td> </tr> <tr> <td>Data A:3</td> <td>Data B:3</td> <td>Data C:3</td> </tr> </table>
Try something like below 尝试如下
$('input:checkbox').click(function() { $(this).parent().addClass('green') $('input:checkbox').not(this).prop('checked', false).parent().removeClass('green'); });
.green { background-color: green; width: 50px; height: 50px; padding: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table width="200" border="1"> <tbody> <tr> <td><input type="checkbox" /></td> <td><input type="checkbox" /></td> <td><input type="checkbox" /></td> <td><input type="checkbox" /></td> <td><input type="checkbox" /></td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.