//Iterate through stuff with for loop method
//http://stackoverflow.com/questions/3065342/how-do-i-iterate-through-table-rows-and-cells-in-javascript
var omitTD = $('.ColHead').closest('tr').find('td:eq(0), td:eq(1)'), headerTD = $('.ColHead').closest('tr').find('td').not(omitTD);
;
var table = document.getElementById("mytab1");
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
headerTD.eq(j).css("background-color", "#ACE");
}
}
headerTD.text('Target This');
omitTD.text('Dont Target');
table {
border-collapse: collapse;
}
td,
tr {
border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table id="mytab1">
<tbody>
<tr>
<td rowspan="2"></td>
<td class="mrGridQuestionText" style=""></td>
<td colspan="3" class="mrGridQuestionText" style="">
<div class="ColHead"></div>
<center>
<span class="mrQuestionText" style=""><div class="ColHead"></div>Carrier
<br>
Selection/
<br>
Negotiation
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Order
<br>
Fulfillment
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Transportation
<br>
Distribution
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Inventory
<br>
Management
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Logistics
<br>
Information
<br>
Systems
</span>
</center>
</td>
</tr>
<tr>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">Have Used</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
</tr>
<tr>
<td rowspan="2"></td>
<td class="mrGridQuestionText" style=""></td>
<td colspan="3" class="mrGridQuestionText" style="">
<div class="ColHead"></div>
<center>
<span class="mrQuestionText" style=""><div class="ColHead"></div>Carrier
<br>
Selection/
<br>
Negotiation
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Order
<br>
Fulfillment
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Transportation
<br>
Distribution
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Inventory
<br>
Management
</span>
</center>
</td>
<td colspan="3" class="mrGridQuestionText" style="">
<center>
<span class="mrQuestionText" style="">Logistics
<br>
Information
<br>
Systems
</span>
</center>
</td>
</tr>
<tr>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">Have Used</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">A</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">B</span>
</td>
<td class="mrGridQuestionText" style="text-Align: Center;vertical-align: Bottom;width: 40px;border-color: black;border-style: Solid;border-width: 1px;">
<span class="mrQuestionText" style="font-size: 9pt;">C</span>
</td>
</tr>
</tbody>
</table>
为什么代码段中的JavaScript代码有效,但不是以下内容:
//Iterate through stuff with for loop method
//http://stackoverflow.com/questions/3065342/how-do-i-iterate-through-table-rows-and-cells-in-javascript
var headerTD = $('.ColHead').closest('tr').find('td').not('td:eq(0), td:eq(1)'),
omitTD = $('.ColHead').closest('tr').find('td:eq(0), td:eq(1)');
var table = document.getElementById("mytab1");
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
headerTD.eq(j).css("background-color", "#ACE");
}
}
headerTD.text('Target This');
omitTD.text('Dont Target');
目标只是将某些css应用于某些单元格,并使用for循环排除前两个单元格。请注意,我故意不使用.each()
这是一个更短的代码,但性能稍慢。
答案 0 :(得分:0)
代码并不是很清楚,但似乎你想要设置第一个表格行中单元格的背景颜色,跳过前两列,是吗?
如果是这样,那么你可以使用CSS。
tr:first-of-type td:nth-child(n+3) {
background-color: #ace;
}
如果第一行实际上是标题行,那么使用<thead>
和<th>
将使CSS和HTML更清晰:
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
<th>baz</th>
</tr>
</thead>
<tbody> ...etc </tbody>
</table>
th:nth-child(n+3) {
background-color: #ace;
}