简体   繁体   English

更改 HTML 表格中整行的背景颜色

[英]Change background color of entire row in HTML table

I have a dynamic table which data's are coming from an API.我有一个动态表,其中数据来自 API。 I'm generating the table and inserting rows using JavaScript.我正在生成表格并使用 JavaScript 插入行。

Everything is working properly but the problem is in styling.一切正常,但问题在于样式。

My table row colors are stripped (just like we use bootstrap table-striped class) and cells are not same in every row.我的表格行颜色被剥离(就像我们使用引导表条纹类一样)并且每一行的单元格都不相同。

Some has 3, some has 6, some 4 etc. Where cells are less they are showing some blank space.有些有 3 个,有些有 6 个,有些有 4 个等等。在单元格较少的地方,它们显示一些空白。

Is there any way i can color the entire row.有什么办法可以给整行上色。 Here is an example.这是一个例子。 Here is an example of my code:这是我的代码示例:

 .table-striped th { height: 45px; background-color: #bfff00 !important; color: #191919; } .table-striped td { padding: 8px; border: 2px solid #F6F6F6; font-weight: bold; } .table-striped>tr:nth-child(n+1)>td { background-color: #bababa; } .table-striped>tr:nth-child(n+2)>td { background-color: #e8e7e6; }
 <div> <table class="table-striped"> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td>Cell 6</td> <td>Cell 7</td> <td>Cell 8</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> </tr> </table> </div>

Here is jsfiddle这是jsfiddle

The table row is only as wide as the number of cells. 表行仅与单元格数一样宽。

Add a final cell when you don't have a full row and use colspan as appropriate. 如果没有完整行,请添加最终单元格,并根据需要使用colspan

 .table-striped th { height: 45px; background-color: #bfff00 !important; color: #191919; } .table-striped td { padding: 8px; border: 2px solid #F6F6F6; font-weight: bold; } .table-striped tr:nth-child(odd) { background-color: #bababa; } .table-striped tr:nth-child(even) { background-color: #e8e7e6; } 
 <div> <table class="table-striped"> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td colspan="5"></td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td>Cell 6</td> <td>Cell 7</td> <td>Cell 8</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td colspan="3"></td> </tr> </table> </div> 

Note from MDN that : MDN提供的注意事项:

Any styles applied to the <tr> element will affect the cells within the row unless overridden by styles applied to those cells. 应用于<tr>元素的任何样式都会影响行中的单元格,除非被应用于这些单元格的样式所覆盖。

Pay particular attention to the fact it styles the cells, not the row itself 要特别注意它对单元格进行样式化的事实,而不是行本身

EDIT 编辑

We know from your question and comments that you are using javascript to generate the table. 我们从您的问题和评论中知道您正在使用javascript生成表格。 We can't infer much on how you are doing this, but if you are generating the table row by row you should be able to see if you have generated the amount of cell for that row equal to the amount of th cells. 我们不能推断出多少你是如何做,但如果你是按行生成的表行,你应该能够看到,如果你已经产生的细胞的量该行相等的量th的细胞。 If not, add a cell with the difference as a colspan . 如果没有,请添加具有差异的单元格作为colspan If you can't do this you can do it after the table is created. 如果您不能这样做,您可以创建表执行此操作。

This is a rough way of doing it 这是一个粗略的方法

 function addColspan(table) { //Assume number of th are our max cells per row var maxCells = table.querySelectorAll("th").length; //Get all rows but the first var rows = table.querySelectorAll("tr:nth-child(n+2)"); for(var i = 0; i < rows.length; i++){ //Get how many cells we have var cellCount = rows[i].querySelectorAll("td").length; //If we don't have enough cells if(cellCount < maxCells) { //Add one with the appropriate colspan rows[i].innerHTML += "<td colspan='" + (maxCells - cellCount) + "'></td>"; } } } //Run after table is generated addColspan(document.querySelector(".table-striped")); 
 .table-striped th { height: 45px; background-color: #bfff00 !important; color: #191919; } .table-striped td { padding: 8px; border: 2px solid #F6F6F6; font-weight: bold; } .table-striped tr:nth-child(odd) { background-color: #bababa; } .table-striped tr:nth-child(even) { background-color: #e8e7e6; } 
 <div> <table class="table-striped"> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td>Cell 6</td> <td>Cell 7</td> <td>Cell 8</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> </tr> </table> </div> 

You can use even and odd properties in css. 您可以在css中使用evenodd属性。 Check this out 看一下这个

 .table-striped th { height: 45px; background-color: #bfff00 !important; color: #191919; } .table-striped td { padding: 8px; border: 2px solid #F6F6F6; font-weight: bold; } .table-striped tr:nth-child(odd) td { background-color: #bababa; } .table-striped tr:nth-child(even) td { background-color: #e8e7e6; } 
 <div> <table class="table-striped"> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td>Cell 6</td> <td>Cell 7</td> <td>Cell 8</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> </tr> </table> </div> 


Updated answer after the comment. 评论后更新了答案。 You need to have alteast empty in order to highlight the complete rows. 您需要将alteast留空以突出显示完整的行。

 .table-striped th { height: 45px; background-color: #bfff00 !important; color: #191919; } .table-striped td { padding: 8px; border: 2px solid #F6F6F6; font-weight: bold; } .table-striped tr:nth-child(odd) td { background-color: #bababa; } .table-striped tr:nth-child(even) td { background-color: #e8e7e6; } 
 <div> <table class="table-striped"> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td>Cell 6</td> <td>Cell 7</td> <td>Cell 8</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> <td>Cell 5</td> <td></td> <td></td> <td></td> </tr> </table> </div> 

Try to give your row an id. 尝试给你的行一个id。 Then you should be able to access the entire row in your css code. 然后,您应该能够访问CSS代码中的整个行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM