這是我的HTML代碼:http://jsfiddle.net/Udxyb/406/如何寫jQuery來展開/摺疊行?
在上面的jQuery代碼中,默認打開表格,當我點擊它時,它會關閉。但我想要的是默認情況下表必須關閉,當我點擊它,然後表必須打開?
任何幫助將不勝感激?
jQuery(function($) {
$("#polls").on("click", ".flip", function() {
$(this)
.closest('tbody')
.next('.section')
.toggle('fast');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="polls" style=" border: 1px solid #ccc;">
<table id="main_table" style="width: 1002px; border: 1px solid #ccc;">
<tbody>
<tr style="background-color:green; color:white">
<td colspan="" class="flip"> Section 1</td>
<td colspan="" class="flip"> Section 2 </td>
<td colspan="" class="flip"> Section 3 </td>
<td colspan="" class="flip"> Section 4 </td>
</tr>
</tbody>
<tbody class="section">
<tr>
<td>item 111</td>
<td>item 112</td>
<td>item 113</td>
<td>item 114</td>
</tr>
<tr>
<td>item 121</td>
<td>item 122</td>
<td>item 123</td>
<td>item 124</td>
</tr>
<tr>
<td>item 131</td>
<td>item 132</td>
<td>item 133</td>
<td>item 134</td>
</tr>
</tbody>
<tbody>
<tr style="background-color:green; color:white">
<td colspan="" class="flip"> Section 1 </td>
<td colspan="" class="flip"> Section 2 </td>
<td colspan="" class="flip"> Section 3 </td>
<td colspan="" class="flip"> Section 4 </td>
</tr>
</tbody>
<tbody class="section">
<tr>
<td>item 211</td>
<td>item 212</td>
<td>item 213</td>
<td>item 214</td>
</tr>
<tr>
<td>item 221</td>
<td>item 222</td>
<td>item 223</td>
<td>item 224</td>
</tr>
<tr>
<td>item 231</td>
<td>item 232</td>
<td>item 233</td>
<td>item 234</td>
</tr>
</tbody>
<tbody>
<tr style="background-color:green; color:white">
<td colspan="" class="flip"> Section 1 </td>
<td colspan="" class="flip"> Section 2 </td>
<td colspan="" class="flip"> Section 3 </td>
<td colspan="" class="flip"> Section 4 </td>
</tr>
</tbody>
<tbody class="section">
<tr>
<td>item 311</td>
<td>item 312</td>
<td>item 313</td>
<td>item 314</td>
</tr>
<tr>
<td>item 321</td>
<td>item 322</td>
<td>item 323</td>
<td>item 324</td>
</tr>
<tr>
<td>item 331</td>
<td>item 332</td>
<td>item 333</td>
<td>item 334</td>
</tr>
</tbody>
</table>
</div>
完美的答案... –
一個CSS的解決方案几乎總是比jQuery的解決方案,每次更好 – MLeFevre
完美的感謝。 ... – user3332446