【发布时间】:2010-10-07 04:07:52
【问题描述】:
jQuery 目前有 .next(filter) 和 .nextAll(filter) 但我需要一些适合它们中间的东西 - 实际上是一个 .nextWhile(filter) 重复执行下一步直到过滤器不再为真,然后停止(而不是继续结束)。
为了证明这一点,下面是一些简化的 HTML——(实际上,它是动态生成的、随机顺序/数据、更多列、正确的类名等等)。
<table>
<thead>
<tr>
<th>...</th>
</tr>
</thead>
<tbody>
<tr class="x"><td>a <button>Show/Hide</button></td></tr>
<tr class="x y"><td>a1</td></tr>
<tr class="x y"><td>a2</td></tr>
<tr class="z"><td>b</td></tr>
<tr class="z"><td>c</td></tr>
<tr class="x"><td>d <button>Show/Hide</button></td></tr>
<tr class="x y"><td>d1</td></tr>
<tr class="x y"><td>d2</td></tr>
<tr class="x y"><td>d3</td></tr>
<tr class="z"><td>e</td></tr>
<tr class="x"><td>f</td></tr>
<tr class="x"><td>g <button>Show/Hide</button></td></tr>
<tr class="x y"><td>g1</td></tr>
<tr class="x y"><td>g2</td></tr>
</tbody>
</table>
针对此运行一些 JavaScript:
<script type="text/javascript">
var $j = jQuery.noConflict();
$j().ready(init);
function init()
{
$j('tr.y').hide();
$j('tr.x button').click( toggleRelated );
}
function toggleRelated()
{
// Only toggles one row
// $j(this).parents('tr').next('.y').toggle();
// Toggles unrelated ones also
$j(this).parents('tr').nextAll('.y').toggle();
// Not currently a jQuery construct
// $j(this).parents('tr').nextWhile('.y').toggle();
}
</script>
有没有简单的方法来实现这个nextWhile 构造?
理想情况下,这需要在不修改当前 HTML 的情况下实现。
【问题讨论】:
标签: javascript jquery jquery-selectors traversal