实现点击是显示表格,再次点击隐藏类名father下的son.对相邻的兄弟元素son没有影响。
html代码:
<table class="table">
<thead>
<tr>
<th>名称</th>
<th>城市</th>
</tr>
</thead>
<tbody>
<tr class="father">
<td>Tanmay</td>
<td>Bangalore</td>
</tr>
<tr class="son">
<td>Sachin</td>
<td>Mumbai</td>
</tr>
<tr class="son">
<td>Sachin</td>
<td>Mumbai</td>
</tr>
<tr class="son">
<td>Sachin</td>
<td>Mumbai</td>
</tr>
<tr class="father">
<td>Tanmay</td>
<td>Bangalore</td>
</tr>
<tr class="son">
<td>Sachin</td>
<td>Mumbai</td>
</tr>
</tbody>
</table>
思路:即选择两个father之间的兄弟元素,先选择到点击事件的father的所有兄弟元素,然后利用循环函数each()逐个循环匹配,遇到father,跳出循环。
<script>
$(document).ready(function() {$('.father').click(function(){
var e = $(this).nextAll();
e.each(function(index, el) {
if(
$(this).attr("class")=='son'){
$(this).slideToggle();
}else{
return false;
}
});
})
});
</script>