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を変更せずに達成する必要があります。