Javascript: Change bgColor of a table cell
I want to change the color of one specific cell in my table: Why does the following not work?
document.getElementById('myTable').rows[i].cells[j].bgColor="#mybgColor";开发者_运维百科
where i
and j
are some integers certainly within the range of the rows and cells.
I don't need any fancy JQuery or so, just this simple command.
CSS styles will override the bgColor
attribute, which is deprecated. Use .style.backgroundColor
instead of .bgColor
:
document.getElementById('myTable').rows[i].cells[j].style.backgroundColor = "#003366";
If you just happen to really like bgColor, or you need to change an attribute; use this:
document.getElementById('myTable').rows[i].cells[j].setAttribute('bgColor', 'red');
setAttribute
This should work
object.style.backgroundColor="#00FF00"
...cells[0].style.bgColor = '#002200';
精彩评论