簡體   English   中英

Table thead color復選框選中/取消選中

[英]Table thead color checkbox check/Uncheck

我有關於復選框與表的簡短問題。 我不想在第一個主題廣告中更改顏色,如何從標題中刪除顏色?

我粘貼代碼,僅使用純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 = "";   
            }
        }
    }

通過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> 

這部分代碼將刪除colorbackground的所有th秒。

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

我認為這部分代碼也是錯誤的。 我將起始索引從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 = "";
  }
}

希望這對您有幫助

 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> 

使用基於主題或正文的條件來設置顏色。 例如,如果checkboxes[i].parent("tbody").is("tbody")然后設置顏色。

如果要從標題中刪除顏色,請不要寫:

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

只寫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM