Javascript:更改表格单元格的颜色

Javascript:更改表格单元格的颜色,javascript,Javascript,我想更改表中某个特定单元格的颜色: 为什么下面的方法不起作用 document.getElementById('myTable').rows[i].cells[j].bgColor="#mybgColor"; 其中,i和j是行和单元格范围内的一些整数。 我不需要任何花哨的JQuery,只要这个简单的命令。CSS样式将覆盖bgColor属性,该属性已被弃用。使用.style.backgroundColor代替.bgColor: ...cells[0].style.bgColor = '#0022

我想更改表中某个特定单元格的颜色: 为什么下面的方法不起作用

document.getElementById('myTable').rows[i].cells[j].bgColor="#mybgColor";
其中,
i
j
是行和单元格范围内的一些整数。
我不需要任何花哨的JQuery,只要这个简单的命令。

CSS样式将覆盖
bgColor
属性,该属性已被弃用。使用
.style.backgroundColor
代替
.bgColor

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

CSS样式将覆盖已弃用的
bgColor
属性。使用
.style.backgroundColor
代替
.bgColor

document.getElementById('myTable').rows[i].cells[j].style.backgroundColor = "#003366";
这应该行得通

object.style.backgroundColor="#00FF00"
这应该行得通

object.style.backgroundColor="#00FF00"

如果你恰好真的喜欢bgColor,或者你需要改变一个属性;使用以下命令:

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

如果您恰好非常喜欢bgColor,或者您需要更改属性;使用以下命令:

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

没有
style.bgColor
css属性。@gilly3:你是对的,我投票给他,因为我忘了css中没有属性bgColor,但谢谢你的更正。没有
style.bgColor
css属性。@gilly3:你是对的,我投票给他,因为我忘了css中没有属性bgColor,但是谢谢你的纠正。