2012-04-16 109 views
4

試圖顯示/隱藏表使用jQuery的行。前兩行工作。第三行不顯示所有數據。jquery顯示/隱藏多個錶行

Jfiddle:http://jsfiddle.net/vcolmenar/wG8qf/1/

HTML爲表 主 數據

<tr class="main"> 
    <td> 
    <a href="#" class="main">12345</a> 
    </td> 
</tr> 

<tr class = "data"> 
    <td> 
    </td> 
    <td> 
     11111 
    </td> 
</tr> 

<tr class="main"> 
    <td> 
     <a href="#" class="main">12345</a> 
    </td> 
</tr> 

<tr class = "data"> 
    <td> 
    </td> 
    <td> 
     11111 
    </td> 
</tr> 

<tr class="main"> 
    <td> 
     <a href="#" class="main">12345</a> 
    </td> 
</tr> 

<tr class = "data"> 
    <td> 
    </td> 
    <td> 
     11111 
    </td> 
</tr> 

<tr class = "data"> 
    <td> 
    </td> 
    <td> 
     22222 
    </td> 
</tr> 
<tr class = "data"> 
    <td> 
    </td> 
    <td> 
     33333 
    </td> 
</tr> 
<tr class = "data"> 
    <td> 
    </td> 
    <td> 
     44444 
    </td> 
</tr> 

JavaScript進行Jquery的顯示/隱藏功能數據

$(document).ready(function() { 
//Hide table rows with class 'min', but appear when clicked. 
$(".data").hide(); 
$(".main").click(function() { 
    $(this).parent().parent().next(".data").toggle("fast"); 
}); 
}); 
+0

沒有'你的第三個主要 – SpYk3HH 2012-04-16 21:53:28

+0

劃傷後.data'那,看起來像你剛剛修改它,它看起來像它的工作,這是什麼問題? – SpYk3HH 2012-04-16 21:54:59

+0

@ SpYk3HH最後一個a.main只顯示第一個兄弟tr.data,我想他想要tr.main條目之間的所有tr.data行 – DefyGravity 2012-04-16 22:11:23

回答

8

CSS

tr.data {顯示:無;}

的JavaScript

$(function(){ 
    $("#main-data-table","body").on({'click':function(event){ 
    event.preventDefault(); 
    $(this).closest("tr.main").nextUntil("tr.main").toggle("fast"); 
    }}, 
    "a.main",null); 
}); 

HTML

<table id="main-data-table"> 
<thead><tr> 
    <th>Main</th> 
    <th>Data</th> 
</tr></thead> 
<tfoot></tfoot> 
<tbody> 
<tr class="main"> 
     <td> 
     <a href="#" class="main">12345</a> 
     </td> 
    </tr> 

    <tr class = "data"> 
     <td> 
     </td> 
     <td> 
      11111 
     </td> 
    </tr> 

    <tr class="main"> 
     <td> 
      <a href="#" class="main">12345</a> 
     </td> 
    </tr> 

    <tr class = "data"> 
     <td> 
     </td> 
     <td> 
      11111 
     </td> 
    </tr> 

    <tr class="main"> 
     <td> 
      <a href="#" class="main">12345</a> 
     </td> 
    </tr> 

    <tr class = "data"> 
     <td> 
     </td> 
     <td> 
      11111 
     </td> 
    </tr> 

    <tr class = "data"> 
     <td> 
     </td> 
     <td> 
      22222 
     </td> 
    </tr> 
    <tr class = "data"> 
     <td> 
     </td> 
     <td> 
      33333 
     </td> 
    </tr> 
    <tr class = "data"> 
     <td> 
     </td> 
     <td> 
      44444 
     </td> 
    </tr> 
</tbody> 
</table> 
+1

[fiddle](http://jsfiddle.net/wG8qf/7/ ) – DefyGravity 2012-04-16 22:04:25

+0

謝謝,這很好! – squeezethejuicebox2 2012-04-17 03:38:21

+0

@DefyGravity偉大的人 – 2015-04-23 06:34:31