2012-08-29 93 views
0

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> 

任何想法?謝謝。

+0

只是爲了澄清......你想要做的就是在當前一個月當表第一次加載強調什麼? – ews2001

+0

正確 - 當月的單元格背景顏色應該改變,以便從其他月份中脫穎而出。 –

回答

0

這個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' 
    } 
} 
+0

謝謝!我明白我缺少的東西。 –