我想改变表格中一个特定单元格的颜色:为什么下面的操作不起作用?
document.getElementById('myTable').rows[i].cells[j].bgColor="#mybgColor";
,其中i
和j
是行和单元格范围内的整数。我不需要任何花哨的JQuery,只需要这个简单的命令。
CSS样式将覆盖bgColor
属性,这是不赞成的。使用 .style.backgroundColor
代替.bgColor
:
document.getElementById('myTable').rows[i].cells[j].style.backgroundColor = "#003366";
如果你只是碰巧真的喜欢bgColor,或者你需要改变一个属性;使用:
document.getElementById('myTable').rows[i].cells[j].setAttribute('bgColor', 'red');
setAttribute
应该可以了
object.style.backgroundColor="#00FF00"
...cells[0].style.bgColor = '#002200';