展开和折叠 HTML table

Expand & collapse HTML table

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

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

理想情况下,单击 "Values 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'>1.11
        </td>
        <td class='expandSUB1'></td>
        <td class='expandSUB1' align='right'>8.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>

嗨,据我所知,你应该在 expandFRED 中切换隐藏 class class点击事件切换整个table下面是总代码

请申请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'>
        1.11
      </td>
      <td class='expandSUB1'></td>
      <td class='expandSUB1' align='right'>8.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>

已编辑:

替换为以下代码:

$(".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'>1.11
        </td>
        <td class='expandSUB1'></td>
        <td class='expandSUB1' align='right'>8.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>