我有这个表,所有.sales-details
行都会被隐藏,但是当你点击.grey
行时,我只希望在出现后立即显示.sales-details
行,直到下一行.grey
行。这对Jquery来说可能吗?
<table width="100%" class="modal-table" id="modal-table">
<thead>
<tr>
<th>Surgeon name</th>
<th>Country</th>
<th>Antiquity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr class="grey"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="grey"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="grey"><td></td></tr>
<tr class="sales-details"><td></td></tr>
<tr class="sales-details"><td></td></tr>
</tbody>
</table>
答案 0 :(得分:1)
将一个点击处理程序附加到灰色行(我可能会委托它),然后在处理程序this
中将引用单击的行。您可以使用nextUntil
选择跟随它的行,直到匹配的选择器:
所以:
$("selector-for-the-table").on("click", "tr.grey", function() {
$(this).nextUntil(".grey").show();
});
示例:
$(".modal-table").on("click", "tr.grey", function() {
$(this).nextUntil(".grey").show();
});
.modal-table .sales-details {
display: none;
}
<table width="100%" class="modal-table" id="modal-table">
<thead>
<tr>
<th>Surgeon name</th>
<th>Country</th>
<th>Antiquity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr class="grey">
<td>grey 1</td>
</tr>
<tr class="sales-details">
<td>detail group 1</td>
</tr>
<tr class="sales-details">
<td>detail group 1</td>
</tr>
<tr class="sales-details">
<td>detail group 1</td>
</tr>
<tr class="grey">
<td>grey 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="grey">
<td>grey 3</td>
</tr>
<tr class="sales-details">
<td>detail group 3</td>
</tr>
<tr class="sales-details">
<td>detail group 3</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
另一个选择是将行分组为tbody
元素,并使用.closest("tbody").next().show()
显示整个tbody:
$("selector-for-the-table").on("click", "tr.grey", function() {
$(this).closest("tbody").next().show();
});
示例:
$(".modal-table").on("click", "tr.grey", function() {
$(this).closest("tbody").next().show();
});
.modal-table .tbody-details {
display: none;
}
<table width="100%" class="modal-table" id="modal-table">
<thead>
<tr>
<th>Surgeon name</th>
<th>Country</th>
<th>Antiquity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr class="grey">
<td>grey 1</td>
</tr>
</tbody>
<tbody class="tbody-details">
<tr class="sales-details">
<td>detail group 1</td>
</tr>
<tr class="sales-details">
<td>detail group 1</td>
</tr>
<tr class="sales-details">
<td>detail group 1</td>
</tr>
</tbody>
<tbody>
<tr class="grey">
<td>grey 2</td>
</tr>
</tbody>
<tbody class="tbody-details">
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
<tr class="sales-details">
<td>detail group 2</td>
</tr>
</tbody>
<tbody>
<tr class="grey">
<td>grey 3</td>
</tr>
</tbody>
<tbody class="tbody-details">
<tr class="sales-details">
<td>detail group 3</td>
</tr>
<tr class="sales-details">
<td>detail group 3</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>