表格颜色复选框选中/取消选中

时间:2018-05-24 19:24:52

标签: javascript html checkbox colors html-table

关于表格的复选框,我有一个简短的问题。我不想改变颜色第一个thead tr,如何从标题中删除颜色?

我粘贴代码,我只使用Javascript pure。

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 = "";   
            }
        }
    }

通过html

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

此部分代码将删除所有color的{​​{1}}和background

th

我认为这部分代码也是错误的。我将起始索引从 2 更改为 0

var th = document.getElementsByTagName('th');
  for (var i = 0; i < th.length; i++) {
  th[i].style.color = "black";
  th[i].style.background = "white";
}

希望这有助于你

&#13;
&#13;
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 = "";
  }
}
&#13;
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";
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用基于thead或tbody的条件来设置颜色。例如如果checkboxes[i].parent("tbody").is("tbody")然后设置颜色。

答案 2 :(得分:0)

如果要从标题中删除颜色,请不要写:

checkboxes[i].parentNode.parentNode.style.background = "";

只写:

checkboxes[i].parentNode.parentNode.style.background = "transparent";