【问题标题】:Expand & collapse HTML table展开和折叠 HTML 表格
【发布时间】:2019-03-12 18:41:39
【问题描述】:

免责声明:我是 HTML、javascript 和 CSS 新手。

我想要做的是有一个可展开/可折叠的 HTML 表格,如下所示。单击“Values 1”应显示“Category 1”(还有其他类别,但为了最小化代码,我只显示了 1 个类别)。然后单击“类别 1”将显示更多子值。问题是当单击“值 1”时,它只会隐藏“类别 1”,同时仍显示子值。任何想法如何通过最少的代码更改轻松更改它,以便单击值 1 隐藏类别 1 和子值。请注意,还有更多类别和子值,因此也需要隐藏它们。

理想情况下,单击“值 1”应该隐藏所有类别和子值,而再次单击它应该将类别和子值恢复到它们的任何状态,无论是隐藏还是可见。

希望这是有道理的。提前致谢

折叠:

扩展(错误):

完全展开:

< script src = "https://code.jquery.com/jquery-1.11.3.min.js" >
  $(document).ready(function() {
    $('[data-toggle="toggle"]').change(function() {
      $(this).parents().next('.hide').toggle();
    });
  });

$(document).ready(function() {
    $(".expandFRED").click(function() {
      $(".expandSUB1").toggle();
    });
    $(".expandVALS").click(function() {
      $(".expandCAT1").toggle();
    });
  }) <
  /script>
.label tr td label {
  display: block;
}

[data-toggle='toggle'] {
  display: none;
}

.expandSUB1 {
  display: none;
}

. {
  display: none;
}

.expandCAT1 {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<body>
  <table class='imagetable' align='center'>
    <caption><b></b></caption>
    <tbody class="labels">
      <tr>
        <td class='expandFRED' colspan='7'>Values 1 (click here to expand/collapse)
        </td>
      </tr>
    </tbody>
    <tbody class="hide">
      <tr>
        <td class='expandSUB1 expandVALS'>Category 1 (click here to expand/collapse)
        </td>
        <td class='expandSUB1'></td>
        <td class='expandSUB1' align='right'>$461.11
        </td>
        <td class='expandSUB1'></td>
        <td class='expandSUB1' align='right'>$428.33</td>
        <td class='expandSUB1' align='right'>-7.11%
        </td>
        <td class='expandSUB1' align='right'>$-32.78</td>
      </tr>
      <tr>
        <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 1</td>
        <td class='expandCAT1' align='right'>0.405582</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1' align='right'>0.405582</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
      </tr>
      <tr>
        <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 2</td>
        <td class='expandCAT1' align='right'>1</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1' align='right'>1</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
      </tr>
      <tr>
        <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 3</td>
        <td class='expandCAT1' align='right'>1.392379</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1' align='right'>1.407569</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
      </tr>
    </tbody>
  </table>
</body>

</html>

【问题讨论】:

  • 尝试在 w3schools.com 网站上搜索 - 那里有很多有用的 html 信息
  • 一些事情,我建议你美化你的代码,因为每个人都很难阅读。你会想要开始使用制表符或空格来清理它,很多时候你会通过使用正确的语法样式来发现问题。
  • @reecec:检查下面给出的答案。
  • @reecec: 希望this是你想要的
  • 谢谢你,Ahsan 太棒了。感谢您清理它并使其正常工作,这是一个巨大的帮助。还要感谢 NikhilKinkar 清理我的代码,非常感谢。

标签: javascript jquery html collapse expand


【解决方案1】:

您好,据我所知,您应该在 expandFRED 中切换隐藏类 切换整个表格的类点击事件如下是总代码

请你应用你的 CSS

$(document).ready(function() {
  $('[data-toggle="toggle"]').change(function() {
    $(this).parents().next('.hide').toggle();
  });
});

$(document).ready(function() {
  $(".expandFRED").click(function() {
    $(".expandSUB1").toggle();
  });
  $(".expandVALS").click(function() {
    $(".expandCAT1").toggle();
  });
  $(".expandFRED").click(function() {
    $(".hide").toggle();
  });
})
.imagetable {
  border: 1px solid black;
}

.label tr td label {
  display: block;
  border: 1px solid black;
}

td {
  border: 1px solid black;
}

[data-toggle='toggle'] {
  display: block;
}


/*.expandSUB1 {
            display: block;
        }

        .expandFRED {
            display: block;
        }

            .expandCAT1 {
                display: block;
            }*/
<table class='imagetable' align='center'>
  <caption><b></b></caption>
  <tbody class="labels">
    <tr>
      <td class='expandFRED' colspan='7'>
        Values 1 (click here to expand/collapse)
      </td>
    </tr>
  </tbody>
  <tbody class="hide">
    <tr>
      <td class='expandSUB1 expandVALS'>
        Category 1 (click here to expand/collapse)
      </td>
      <td class='expandSUB1'></td>
      <td class='expandSUB1' align='right'>
        $461.11
      </td>
      <td class='expandSUB1'></td>
      <td class='expandSUB1' align='right'>$428.33</td>
      <td class='expandSUB1' align='right'>
        -7.11%
      </td>
      <td class='expandSUB1' align='right'>$-32.78</td>
    </tr>
    <tr>
      <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 1</td>
      <td class='expandCAT1' align='right'>0.405582</td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1' align='right'>0.405582</td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1'></td>
    </tr>
    <tr>
      <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 2</td>
      <td class='expandCAT1' align='right'>1</td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1' align='right'>1</td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1'></td>
    </tr>
    <tr>
      <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 3</td>
      <td class='expandCAT1' align='right'>1.392379</td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1' align='right'>1.407569</td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1'></td>
      <td class='expandCAT1'></td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 感谢您的回复 LDS 我认为您的回答非常好,似乎工作得很好,但已将另一个回复标记为正确答案,因为我只能输入 1 个正确答案。感谢您在这方面的时间
【解决方案2】:

已编辑:

替换为以下代码:

$(".expandFRED").click(function() {
  $('.hide').toggle();  
});
$(".expandVALS").click(function() {
  $(".expandCAT1").toggle();
});

CSS 更改

.hide {
  display: none;
}

并删除以下内容:

.expandSUB1 {
   display: none;
}

$(document).ready(function() {
    $(".expandFRED").click(function() {
      $('.hide').toggle();  
    });
    $(".expandVALS").click(function() {
      $(".expandCAT1").toggle();
    });
  })
.hide {
  display: none;
}

.expandCAT1 {
  display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<body>
  <table class='imagetable table' align='center'>
    <caption><b></b></caption>
    <tbody class="labels">
      <tr>
        <td class='expandFRED' colspan='7'>Values 1 (click here to expand/collapse)
        </td>
      </tr>
    </tbody>
    <tbody class="hide">
      <tr>
        <td class='expandSUB1 expandVALS'>Category 1 (click here to expand/collapse)
        </td>
        <td class='expandSUB1'></td>
        <td class='expandSUB1' align='right'>$461.11
        </td>
        <td class='expandSUB1'></td>
        <td class='expandSUB1' align='right'>$428.33</td>
        <td class='expandSUB1' align='right'>-7.11%
        </td>
        <td class='expandSUB1' align='right'>$-32.78</td>
      </tr>
      <tr>
        <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 1</td>
        <td class='expandCAT1' align='right'>0.405582</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1' align='right'>0.405582</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
      </tr>
      <tr>
        <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 2</td>
        <td class='expandCAT1' align='right'>1</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1' align='right'>1</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
      </tr>
      <tr>
        <td class='expandCAT1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SubValue 3</td>
        <td class='expandCAT1' align='right'>1.392379</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1' align='right'>1.407569</td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
        <td class='expandCAT1'></td>
      </tr>
    </tbody>
  </table>
</body>

</html>

【讨论】:

  • 谢谢这是最好的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 2016-04-18
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多