最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Change bgColor of a table cell - Stack Overflow

programmeradmin1浏览0评论

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 mand.

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 mand.

Share Improve this question edited Sep 16, 2018 at 8:41 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 22, 2011 at 16:54 ptikobjptikobj 2,7107 gold badges39 silver badges65 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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';
发布评论

评论列表(0)

  1. 暂无评论