gpt4 book ai didi

javascript - 尝试对表中的任意行进行 Zebra 条纹

转载 作者:行者123 更新时间:2023-11-28 12:48:47 25 4
gpt4 key购买 nike

我有一个表,其中包含由类标记的行。这些行应该是斑马条纹。需要注意的是它们并不总是连续的。

我尝试过按表和类名进行匹配,但最终结果总是不正确。发生的情况是, strip 化将应用于表级别,然后仅在具有该类的行上启用。

我需要改变什么才能让这项工作按我的预期进行?

脚本

function FormatTable()
{
$("#TableId .arbitrarySelector:nth-child(2n+1)" ).addClass('anotherEquallySpecialRow')
}

HTML

<table id="TableId" onclick="FormatTable()">
<tr class="arbitrarySelector">
<td><div class="space">Should be Changed and is</div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should Not be Changed and is Not</div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should be Changed and is</div></td>
</tr>
<tr>
<td><div class="space"></div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should NOT be changed but is</div></td>
</tr>
<tr>
<td><div class="space"></div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should be Changed and is</div></td>
</tr>
<tr>
<td><div class="space"></div></td>
</tr>
<tr>
<td><div class="space"></div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should Not be Changed and is Not</div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should be Changed and is</div></td>
</tr>
<tr class="arbitrarySelector">
<td><div class="space">Should Not be Changed and is Not</div></td>
</tr>
</table>

最佳答案

你可能让这件事变得太困难了。如果你想做斑马条纹,为什么不直接使用类似 :odd selector 的东西呢?并根据情况更改类(class)。像这样的事情...

// Add zebra striping.
$('.arbitrarySelector:odd').addClass('zebra');

然后 CSS 会是这样的......

.zebra 
{
background-color: #dddddd;
}

关于javascript - 尝试对表中的任意行进行 Zebra 条纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4501359/

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