【问题标题】:change cell background color更改单元格背景颜色
【发布时间】:2012-08-29 18:43:44
【问题描述】:

http://jsfiddle.net/mannagod/Zq6w4/

    now = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
cells = document.getElementById('months').getElementsByTagName('td');
for (c = 0; c < cells.length; c++) {
    if (cells[c].firstChild.nodeValue == now) {
        cells[c].style.backgroundColor = 'red' //today's scripture reading
        cells[c].style.color = 'white'
    }
}

我想更改当前月份单元格的背景和字体颜色。我为下表尝试了上面的代码,但我似乎无法使其工作......

<table class="hovertable" id="hovertable" name="hovertable">

<tbody id="months">
    <tr>
        <td class="style2">
            <a href="01.htm">Jan</a></td>
        <td class="style2">
            <a href="02.htm">Feb</a></td>
        <td class="style2">
            <a href="03.htm">Mar</a></td>
        <td class="style2">
            <a href="04.htm">Apr</a></td>
        <td class="style2">
            <a href="05.htm">May</a></td>
        <td class="style2">
            <a href="06.htm">Jun</a></td>
    </tr>
    <tr>
        <td class="style2">
            <a href="07.htm">Jul</a></td>
        <td class="style2">
            <a href="08.htm">Aug</a></td>
        <td class="style2">
            <a href="09.htm">Sep</a></td>
        <td class="style2">
            <a href="10.htm">Oct</a></td>
        <td class="style2">
            <a href="11.htm">Nov</a></td>
        <td class="style2">
            <a href="12.htm">Dec</a></td>
    </tr>
        </tbody>
    </table>

有什么想法吗?谢谢。

【问题讨论】:

  • 只是为了澄清......您想要做的是在表格首次加载时突出显示当前月份?
  • 正确 - 当前月份的单元格背景颜色应该改变,以便与其他月份脱颖而出。

标签: colors background


【解决方案1】:

这个 javascript 应该做你想做的事:

now = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var d = new Date();
cells = document.getElementById('months').getElementsByTagName('td');
for (c = 0; c < cells.length; c++) {
    if (c == d.getMonth()) {
        cells[c].style.backgroundColor = 'red' //today's scripture reading
        cells[c].style.color = 'white'
    }
}

【讨论】:

  • 谢谢!我明白我错过了什么。
猜你喜欢
  • 2011-09-24
  • 2013-04-12
  • 2021-10-31
  • 1970-01-01
  • 2013-06-15
  • 2013-06-28
  • 2016-05-27
  • 1970-01-01
  • 2020-02-02
相关资源
最近更新 更多