繁体   English   中英

更改单元格背景色

[英]change cell background color

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>

有任何想法吗? 谢谢。

这个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'
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM