我想知道在找到第一个<hr/>
之前如何隐藏所有元素。然后循环应该停止
<div class='row-father'>
<legend> Title here </legend>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<hr/>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<hr/>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<hr/>
</div>
...
$('.row-father').find('*').each(function(){
if ($(this) != '<hr/>')
$(this).hide();
});
答案 0 :(得分:9)
使用此:$('.row-father > hr:first').prevAll(':not(legend)').hide();
请参阅http://api.jquery.com/prevall/
答案 1 :(得分:1)
您可以使用.is('hr')
检测到这一点。
代码:
$('.row-father').find('*').each(function() {
if (!$(this).is('hr')) {
$(this).hide();
}
else {
return false;
}
});
可以找到示例here。
答案 2 :(得分:1)
$('.row-father').find('hr').eq(0).prevAll('.row-fluid').hide();
答案 3 :(得分:0)
您也可以使用
$('.row-fluid:first').nextUntil("hr").andSelf().hide();
修改:
如果您想用包含div的文本替换<hr/>
,例如:
<div class='row-father'>
<legend> Title here </legend>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<div>test</div>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<hr/>
<div class="row-fluid">...</div>
<div class="row-fluid">...</div>
<hr/>
</div>
你可以试试这段代码:
$('.row-fluid:first').nextUntil("div>:contains('test')").andSelf().hide();