展开和折叠HTML表

时间:2018-10-08 06:18:01

标签: javascript jquery html collapse expand

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

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

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

希望这是有道理的。预先感谢

崩溃:

Collapsed version

展开(错误):

Expanded version

完全扩展: enter image description here

< 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>

2 个答案:

答案 0 :(得分:1)

您好,据我所知,您应该在expandFRED中切换hide类 用于切换整个表格的class click事件是总代码

请您应用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>

答案 1 :(得分:0)

已编辑

替换为以下代码:

$(".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>