在下面的代码中,我希望看到显示红色、蓝色、绿色和棕色行,但似乎除了红色之外的所有行都被隐藏,就像nextUntil参数被忽略一样。有什么线索吗?谢谢。
在一些研究之后,我尝试了这两种方法
$("#firstrow").nextUntil('span[class="hues"]').hide();
和
$("#firstrow").nextUntil('span:not(.colors)').hide();
HTML:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"/>
<script>
$(document).ready(function() {
$("#firstrow").nextUntil('span[class="hues"]').hide();
//$("#firstrow").nextUntil('span:not(.colors)').hide();
});
</script>
</head>
<body>
<table id="colortable">
<tr id="firstrow">
<span class="colors">Red</span>
</tr>
<tr>
<span class="colors">Orange</span>
</tr>
<tr>
<span class="hues">Blue</span>
</tr>
<tr>
<span class="shades">Green</span>
</tr>
<tr>
<span class="colors">Brown</span>
</tr>
</table>
</body>
</html>
发布于 2013-05-01 16:17:50
您的标记无效,tr
元素应该只有td
子元素。
<table id="colortable">
<tbody>
<tr id="firstrow">
<td>
<span class="colors">Red</span>
</td>
</tr>
...
</tbody>
</table>
跨度元素不是所选元素的同级元素,nextUntil
仅筛选所选元素的下一个同级元素,您应该选择具有该特定跨度元素的tr
:
// Select the all next sibling elements until the first element that matches the selector
$("#firstrow").nextUntil('tr:has(span.hues)').hide();
如果要选择与选择器匹配的所有下一个同级元素,可以使用nextAll
方法:
// Select the all next sibling elements that match the selector
$("#firstrow").nextAll('tr:has(span.hues)').hide();
https://stackoverflow.com/questions/16321811
复制相似问题