2014-03-07 137 views
0

这是我的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>

回答

2

在你的CSS只需添加

.section { 
    display: none 
} 

+0

完美的答案... –

+0

一个CSS的解决方案几乎总是比jQuery的解决方案,每次更好 – MLeFevre

+0

完美的感谢。 ... – user3332446

0

尝试

$(".section").hide(); 
    jQuery(function($) { 
    $("#polls").on("click", ".flip", function() { 
     $(this) 
     .closest('tbody') 
     .next('.section') 
     .toggle('fast'); 
    }); 
    }); 
}); 

DEMO

0

我更新了代码。现在请检查

$('#main_table').find('.section').hide(); 

$('.flip').click(function() { 
    $(this) 
    .closest('tbody') 
    .next('.section') 
    .toggle('fast'); 
}); 
+0

我更新了jsfiddle中的代码检查http://jsfiddle.net/Udxyb/409/ – Jak

0

您的jQuery(函数($)代码前加上这一句:

$(".section").hide();