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

html table - Javascript change style of td element using tr id - Stack Overflow

programmeradmin5浏览0评论

I have one table:

<table>
    <tr id="436">
       <td>1</td>
       <td>2</td>
       <td>3</td>
    </tr>
</table>

Now I need using Javascript to get tr element using id and then in that tr add CSS style of changing text color.

I have try getelementbyid("436") but I don't know how to do next.

So I need to get this:

  <table>
    <tr id="436">
       <td style="color: red">1</td>
       <td style="color: red">2</td>
       <td style="color: red">3</td>
    </tr>
  </table>

I have one table:

<table>
    <tr id="436">
       <td>1</td>
       <td>2</td>
       <td>3</td>
    </tr>
</table>

Now I need using Javascript to get tr element using id and then in that tr add CSS style of changing text color.

I have try getelementbyid("436") but I don't know how to do next.

So I need to get this:

  <table>
    <tr id="436">
       <td style="color: red">1</td>
       <td style="color: red">2</td>
       <td style="color: red">3</td>
    </tr>
  </table>
Share Improve this question edited Jan 1, 2017 at 17:24 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 30, 2013 at 16:34 user2631534user2631534 4774 gold badges9 silver badges27 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 8

How about this, if you really want to change the color of the td elements:

var tr = document.getElementById("436");
var tds = tr.getElementsByTagName("td");

for(var i = 0; i < tds.length; i++) {
   tds[i].style.color="red";
}

http://jsfiddle/UEbCL/

it should be something like this:

 document.getElementById("436").style.color="red"
发布评论

评论列表(0)

  1. 暂无评论