2011-07-22 48 views
0

我想改變一個特定的單元格的顏色在我的表的變化BGCOLOR?的Javascript: 爲什麼下面不工作:表格單元格

document.getElementById('myTable').rows[i].cells[j].bgColor="#mybgColor"; 

其中i和j肯定是行和單元格範圍內的某些整數。 我不需要任何花哨的JQuery,只是這個簡單的命令。

回答

4

CSS樣式將覆蓋bgColor屬性,它被廢棄了。使用.style.backgroundColor,而不是.bgColor

document.getElementById('myTable').rows[i].cells[j].style.backgroundColor = "#003366"; 
+0

+1對第一個答案進行更正。 –

0
...cells[0].style.bgColor = '#002200'; 
+0

沒有'style.bgColor' CSS屬性。 – gilly3

+0

@ gilly3:你是對我給予好評他,我忘了,沒有屬性的bgcolor ** **在CSS,但感謝你的更正。 –

1

這應該工作

object.style.backgroundColor="#00FF00" 
0

如果你恰好很喜歡的bgcolor,或者您需要更改的屬性;使用此:

document.getElementById('myTable').rows[i].cells[j].setAttribute('bgColor', 'red'); 

setAttribute

相關問題