gpt4 book ai didi

javascript - for 表行中的循环不一致

转载 作者:行者123 更新时间:2023-11-28 06:35:55 24 4
gpt4 key购买 nike

//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(),它的代码要短得多,但性能稍微慢一些。

最佳答案

代码不是很清楚,但是看起来你想设置表格第一行单元格的背景颜色,跳过前两列,是吗?

如果是这样,那么您可以只使用 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;
}

关于javascript - for 表行中的循环不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34686499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com