gpt4 book ai didi

jquery - 使用 jQuery 选择基于类的表格单元格

转载 作者:太空宇宙 更新时间:2023-11-03 23:50:52 24 4
gpt4 key购买 nike

我有一个类似这样的布局:

<tr>
<td class="topCategoryRow"><a href="javascript:void(0);" class="catlink">Some Category</a></td>
</tr>
<tr>
<td class="subCategoryRow">A sub category under Some Category</td>
</tr>
<tr>
<td class="subCategoryRow">Another sub category under Some Category</td>
</tr>
<tr>
<td class="topCategoryRow"><a href="javascript:void(0);" class="catLink">Another Category</a></td>
</tr>
<tr>
<td class="subCategoryRow">A sub category under Another Category</td>
</tr>
<tr>
<td class="subCategoryRow">Another sub category under Another Category</td>
</tr>

我想做的是在 catLink 类上附加一个点击处理程序。我希望能够选择 topCategoryRows 下的 subCategoryRows:

<script type="text/javascript">
$(function() {
$(".catLink").click(function() {
$(".subCategoryRow").show(); // This selector is wrong
});
});
</script>

我的代码选择了所有 subCategoryRow。我只想选择所选类别下的那些。有人可以帮我构建正确的选择器吗?

最佳答案

尝试

$(function() {
$(".catlink").click(function() {
$(this).closest('tr').nextUntil("tr:has(.catlink)").find('.subCategoryRow').show();
});
});

演示:Fiddle

注意:类名区分大小写 - 您使用了 catlinkcatLink

关于jquery - 使用 jQuery 选择基于类的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198706/

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