使用jQuery来slideToggle一组表格行 [英] Using jQuery to slideToggle a group of Table Rows

查看:21
本文介绍了使用jQuery来slideToggle一组表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 javaScript 和 jQuery 还很陌生,所以希望这将是一个快速修复.我需要显示一个包含可分为多个类别的数据的表格,并且我想实现一个 slideToggle 来隐藏/显示每个给定类别中的所有观察结果.

下面的代码应该(理想情况下)显示一个有 4 列和 9 行的表格,每组 3 行前面有一个绿色的第 i 部分"行.我希望每个部分标题都可以作为一个 SlideToggle 来展开或折叠它下面的所有行.现在,没有任何东西正在崩溃.有什么想法吗?

<style type="text/css">td{padding:5px;}</风格><script type="text/javascript" src="js/jquery.js"></script><script type="text/javascript">$(document).ready(function(){$(".flip").click(function(){$(this).next(".section").slideToggle();});});<身体><p><table id="main_table"><头><tr class="firstline"><th>Column1</th><th>Column2</th>第 3 列第 3 列第 4 列第 4 列</tr></thead><tr style="background-color:green; color:white"><td colspan="4" class="flip">第 1 节 </td></tr><div class="section"><tr><td>项目 111</td><td>项目 112</td><td>项目 113</td><td>项目 114</td></tr><tr><td>项目 121</td><td>项目 122</td><td>项目 123</td><td>项目 124</td></tr><tr><td>项目 131</td><td>项目 132</td><td>项目 133</td><td>项目 134</td></tr>

<tr style="background-color:green; color:white"><td colspan="4" class="flip">第 2 节 </td></tr><div class="section"><tr><td>项目 211</td><td>项目 212</td><td>项目 213</td><td>项目 214</td></tr><tr><td>项目 221</td><td>项目 222</td><td>项目 223</td><td>项目 224</td></tr><tr><td>项目 231</td><td>项目 232</td><td>项目 233</td><td>项目 234</td></tr>

<tr style="background-color:green; color:white"><td colspan="4" class="flip">第 3 节 </td></tr><div class="section"><tr><td>项目 311</td><td>项目 312</td><td>项目 313</td><td>项目 314</td></tr><tr><td>项目 321</td><td>项目 322</td><td>项目 323</td><td>项目 324</td></tr><tr><td>项目 331</td><td>项目 332</td><td>项目 333</td><td>项目 334</td></tr>

</tbody></p>

解决方案

你不能把

s 混合成一个 这样的,使用额外的 元素代替.在你的回调中,this
元素,它没有兄弟元素,所以 .next 没有任何用处;您想返回,直到找到与您感兴趣的 .section 相同深度的内容,然后从那里调用 .next.

您的 HTML 应该看起来更像这样:

<头><tr class="firstline"><th>Column1</th><th>Column2</th>第 3 列第 3 列第 4 列第 4 列</tr></thead><tr style="background-color:green; color:white"><td colspan="4" class="flip">第 1 节 </td></tr></tbody><tbody class="section"><tr><td>项目 111</td><td>项目 112</td><td>项目 113</td><td>项目 114</td></tr><!-- ... -->

你的点击处理程序是这样的:

$('.flip').click(function() {$(这个).closest('tbody').next('.section').toggle('快');});

.closest 调用会返回您的祖先,直到找到

然后你调用 .next .

更新 jsfiddle:http://jsfiddle.net/ambiguous/Udxyb/4/

I'm fairly new to javaScript and jQuery, so hopefully this will be a quick fix. I need to display a table containing data that can be grouped into several categories, and I'd like to implement a slideToggle that hides/reveals all of the observations in each given category.

The code below should (ideally) display a table with 4 columns and 9 rows, with every group of 3 rows preceded by a green "Section i" row. I would like each Section header to work as a slideToggle that expands or collapses all of the rows beneath it. Right now, nothing is collapsing. Any thoughts?

<head>
  <style type="text/css">
    td{padding:5px;}
  </style>

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript"> 
      $(document).ready(function(){
      $(".flip").click(function(){
          $(this).next(".section").slideToggle();
      });
  });
  </script>

</head>

<body>
    <p>
        <table id="main_table">
        <thead>
            <tr class="firstline">
                <th>Column1</th>
                <th>Column2</th>
                <th>Column3</th>
                <th>Column4</th>
            </tr>
        </thead>
        <tbody>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 1 </td> 
            </tr>
            <div 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>
            </div>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 2 </td> 
            </tr>
            <div 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>
            </div>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 3 </td> 
            </tr>
            <div 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>
            </div>
        </tbody>
        </table>
    </p>
</body>

解决方案

You can't mix <div>s into a <table> like that, use additional <tbody> elements instead. In your callback, this is the <td> element which has no siblings so .next does nothing useful; you want to go back up until you get to something that is at the same depth as the .section you're interested in and then call .next from there.

Your HTML should look more like this:

<table id="main_table">
    <thead>
        <tr class="firstline">
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
            <th>Column4</th>
        </tr>
    </thead>
    <tbody>
        <tr style="background-color:green; color:white">
            <td  colspan="4" class="flip"> Section 1 </td>
        </tr>
    </tbody>
    <tbody class="section">
        <tr>
            <td>item 111</td>
            <td>item 112</td>
            <td>item 113</td>
            <td>item 114</td>
        </tr>
        <!-- ... -->

And your click handler like this:

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

The .closest call goes back up your ancestors until it finds a <tbody> and then you call .next on that.

Updated jsfiddle: http://jsfiddle.net/ambiguous/Udxyb/4/

这篇关于使用jQuery来slideToggle一组表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆