如果以下所有行都包含特定类,如何隐藏子标题行。当下一个子标题出现时,以下行停止。如何为下一个子标题执行此操作?
示例:隐藏SUBHEADER 2行,因为以下所有行都包含“no”。
<table>
<tr class='sub'>
<td colspan='3'>SUBHEADER 1</td>
</tr>
<tr class='row yes'>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
<tr class='row no'>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
<tr class='sub'>
<td colspan='3'>SUBHEADER 2</td>
</tr>
<tr class='row no'>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
<tr class='row no'>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
<tr class='sub'>
<td colspan='3'>SUBHEADER 3</td>
</tr>
<tr class='row yes'>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
</table>
答案 0 :(得分:1)
这应该这样做
// for each subheader row
$('tr.sub').each(function(){
var self = $(this),
// find all following rows until the next subheader
rows = self.nextUntil('.sub'),
// check if any of those rows contains a .no class
hasNo = rows.find('.no').length > 0;
// hide subheader if no .no class was found
self.toggle(hasNo);
});
更新(在OP 中澄清后)
您需要比较以下行数(,如前所述)是否等于跟随.no
行的数量。
// for each subheader row
$('tr.sub').each(function(){
var self = $(this),
// find all following rows until the next subheader
rows = self.nextUntil('.sub'),
// check if rows number is equal with those of them that have the .no class
allAreNo = rows.filter('.no').length === rows.length;
// show/hide based on whether all are .no or not
self.toggle( !allAreNo );
});
答案 1 :(得分:0)
工作演示 http://jsfiddle.net/5kacR/
API:.find
:http://api.jquery.com/find/
休息应该有助于您的事业:)
代码
$(document).ready(function () {
$("table tr").each(function () {
if(!$(this).find('td.no').length)
$(this).prev('tr').hide();
});
});