【问题标题】:td content Javascript change color and replacetd content Javascript改变颜色和替换
【发布时间】:2017-08-10 19:50:23
【问题描述】:

所以,我有一个带有文字的表格。表中有<th><td>。在某些单元格上,根据单元格的文本,我希望背景更改某种颜色,如果某个特定单元格包含“+”号,我想更改颜色并删除“+”号。

这就是我所拥有的。当它到达一个空单元格并且“+”替换过于严格时,它会中断,即它只会在单元格中唯一的内容时删除“+”。如果是“8+”,则不会删除“+”号。

我是 Javascript 的新手。我相信这可以做得更简单。

<table>
<tr>
    <th>SAT</th>
    <th>SUN</th>
</tr>
<tr>
    <td>K</td>
    <td> </td>
    <td>8+</td>
</tr>
</table>
<script type="text/javascript">
function isEmpty( el ){
  return !$.trim(el.html())
  }


var allTableCells = document.getElementsByTagName("td");
for(var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];

//get the text from the first child node - which should be a text node
var currentText = node.childNodes[0].nodeValue; 

//check for certain content and assign this table cell's background color accordingly 
if (!isEmpty($(currentText))) {
if (currentText === "K")
    node.style.backgroundColor = "#ffff00";
else if (currentText === "+")
    node.style.backgroundColor = "#0070c0";node.childNodes[0].nodeValue = currentText.replace("+", " ");
}
}

var allTableHeaders = document.getElementsByTagName("th");
for(var ic = 0, max = allTableHeaders.length; ic < max; ic++) {
var node = allTableHeaders[ic];

//get the text from the first child node - which should be a text node
var currentHeadText = node.childNodes[0].nodeValue; 

//check for certain days of the week and assign this table cell's background color accordingly 
if (!isEmpty($(currentHeadText))) {
if (currentHeadText === "SAT")
    node.style.backgroundColor = "#ff0000";
else if (currentHeadText === "SUN")
    node.style.backgroundColor = "#91CF4F";
}
}
</script>

【问题讨论】:

    标签: javascript jquery html html-table nodes


    【解决方案1】:

    我认为您想检查 currentText 是否包含 +,而不是检查它们是否相等,

    if (currentText.indexOf("+") > -1)
        node.style.backgroundColor = "#0070c0";
        node.childNodes[0].nodeValue = currentText.replace("+", " ");
    }
    

    【讨论】:

    • 是的,我确实想要那个。谢谢,但我的另一个问题是 td 什么都没有。在我的示例中,我有一个空格,但如果没有空格,它会破坏它并停止检查其他 td。
    • node.childNodes[0].nodeValue 为空时&lt;td&gt;&lt;/td 而不是&lt;td&gt; &lt;/td&gt; 它不会再进一步​​了。
    • 检查是否为空,如果是,什么都不做
    • 我不知道如何检查节点是否为空:/
    • 好的,这个 jQuery 完全符合我的要求,但是将它放在 Wordpress 中不起作用。那么,任何人都可以帮我将其转换为 Javascript 吗? $("table.schedule th:contains('SAT')").css('background-color', '#ff0000'); $("table.schedule th:contains('SUN')").css('background-color', '#91CF4F'); $("table.schedule td:contains('K')").css('background-color', '#ffff00'); $("table.schedule td:contains('*')").css('background-color', '#0070c0'); $(document).ready(function() { var strNewString = $('body').html().replace(/\*/g,''); $('body').html(strNewString); });
    【解决方案2】:

    只需要一些修改... 你使用 isEmpty 的方式,你不需要在它的参数上调用 .html() 。此外,正如前面的答案所建议的,使用 jQuery indexOf() 测试 currentText 中“+”的包含情况。

    function isEmpty( el ){
      return !$.trim(el)
    }
    
    var allTableCells = document.getElementsByTagName("td");
    for(var i = 0, max = allTableCells.length; i < max; i++) {
        var node = allTableCells[i];
    
        //get the text from the first child node - which should be a text node
        var $currentText = node.childNodes[0].nodeValue; 
    
        //check for certain content and assign this table cell's background color accordingly 
        if (!isEmpty($currentText)) {
            if ($currentText === "K")
                node.style.backgroundColor = "#ffff00";
            else if ($currentText.indexOf("+") != -1){
                node.style.backgroundColor = "#0070c0";
                node.childNodes[0].nodeValue = $currentText.replace("+", " ");
            }
        }
    }
    
    var allTableHeaders = document.getElementsByTagName("th");
    for(var ic = 0, max = allTableHeaders.length; ic < max; ic++) {
        var node = allTableHeaders[ic];
    
        //get the text from the first child node - which should be a text node
        var currentHeadText = node.childNodes[0].nodeValue; 
    
        //check for certain days of the week and assign this table cell's background color accordingly 
        if (!isEmpty(currentHeadText)) {
            if (currentHeadText === "SAT")
                node.style.backgroundColor = "#ff0000";
            else if (currentHeadText === "SUN")
                node.style.backgroundColor = "#91CF4F";
        }
    }
    

    【讨论】:

    • 我把它放进去,但它在 K 之后停止并且不会为任何单元格着色并且不会在 K 之后检查其他单元格。第一次检查是在 td 上,所以在 K 之后的 td 中遇到 null 后,它不会继续前进。
    • 所以如果你有 '' 而不是 ' ' 它就不起作用。
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2017-09-21
    • 2013-11-05
    • 2016-08-12
    • 2014-01-12
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多