[英]Table thead color checkbox check/Uncheck
I have short issue about checkbox all with the table. 我有关于复选框与表的简短问题。 I no want change color the first thead tr, how remove the color from the header? 我不想在第一个主题广告中更改颜色,如何从标题中删除颜色?
I paste the code and I use only Javascript pure. 我粘贴代码,仅使用纯Javascript。
function allpart(chk){
var parent = chk.parentNode.parentNode;
var checkboxes = document.getElementsByTagName('input');
var chkpart = document.getElementById('idpart');
if(chk.checked){
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].type == 'checkbox'){
checkboxes[i].checked = true;
checkboxes[i].parentNode.parentNode.style.background = "#CC0000";
checkboxes[i].parentNode.parentNode.style.color = "#FFF";
}
}
}else{
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].type == 'checkbox'){
checkboxes[i].checked = false;
checkboxes[i].parentNode.parentNode.style.background = "";
checkboxes[i].parentNode.parentNode.style.color = "";
}
}
}
Via html 通过HTML
<table align="center" width="540px" class="tbcuadro"> <thead> <tr> <th width="5%"><input name="idpart[]" id="idpart" type="checkbox" onclick="allpart(this);"></th> <th width="75%" align="center">NAME</th> <th width="20%" align="center">NUMBER</th> </tr> </thead> <tbody> <tr> <td width="5%"><input name="idpart" type='checkbox'></td> <td width="75%">row 1</td> <td width="20%">row 2</td> </tr> <tr> <td width="5%"><input name="idpart" type='checkbox'></td> <td width="75%">row 1</td> <td width="20%">row 2</td> </tr> <tr> <td width="5%"><input name="idpart" type='checkbox'></td> <td width="75%">row 1</td> <td width="20%">row 2</td> </tr> </tbody> </table>
This part of code will remove color
and background
of all th
s. 这部分代码将删除color
和background
的所有th
秒。
var th = document.getElementsByTagName('th');
for (var i = 0; i < th.length; i++) {
th[i].style.color = "black";
th[i].style.background = "white";
}
Also this part of code I think is wrong. 我认为这部分代码也是错误的。 I change the start index from 2 to 0 我将起始索引从2更改为0
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type == "checkbox") {
checkboxes[i].checked = false;
checkboxes[i].parentNode.parentNode.style.background = "";
checkboxes[i].parentNode.parentNode.style.color = "";
}
}
Hope this helps you 希望这对您有帮助
function allpart(chk) { var parent = chk.parentNode.parentNode; var checkboxes = document.getElementsByTagName("input"); var chkpart = document.getElementById("idpart"); if (chk.checked) { for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].type == "checkbox") { checkboxes[i].checked = true; checkboxes[i].parentNode.parentNode.style.background = "#CC0000"; checkboxes[i].parentNode.parentNode.style.color = "#FFF"; } } } else { for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].type == "checkbox") { checkboxes[i].checked = false; checkboxes[i].parentNode.parentNode.style.background = ""; checkboxes[i].parentNode.parentNode.style.color = ""; } } } var th = document.getElementsByTagName('th'); for (var i = 0; i < th.length; i++) { th[i].style.color = "black"; th[i].style.background = "white"; } }
<table align="center" width="540px" class="tbcuadro"> <thead> <tr> <th width="5%"><input name="idpart[]" id="idpart" type="checkbox" onclick="allpart(this)"></th> <th width="75%" align="center">NAME</th> <th width="20%" align="center">NUMBER</th> </tr> </thead> <tbody> <tr> <td width="5%"><input name="idpart" type='checkbox'></td> <td width="75%">row 1</td> <td width="20%">row 2</td> </tr> <tr> <td width="5%"><input name="idpart" type='checkbox'></td> <td width="75%">row 1</td> <td width="20%">row 2</td> </tr> <tr> <td width="5%"><input name="idpart" type='checkbox'></td> <td width="75%">row 1</td> <td width="20%">row 2</td> </tr> </tbody> </table>
Use a conditional based on thead or tbody to set the color. 使用基于主题或正文的条件来设置颜色。 eg if checkboxes[i].parent("tbody").is("tbody")
then set the color. 例如,如果checkboxes[i].parent("tbody").is("tbody")
然后设置颜色。
If you want to remove the color from the header don't write: 如果要从标题中删除颜色,请不要写:
checkboxes[i].parentNode.parentNode.style.background = "";
write just: 只写:
checkboxes[i].parentNode.parentNode.style.background = "transparent";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.