jQuery SlideToggle 在带有 thead 和 tbody 元素的表格上。没有动画。

发布于 2024-11-27 02:59:36 字数 851 浏览 5 评论 0原文

我有一张带有 head 和 tbody 部分的桌子。我已成功应用了 SlideToggle,但动画已损坏。

当用户单击 thead 时,我希望 tbody 的内容向上滑动。目前发生的情况是该部分完全消失,没有任何动画。

这是表格

<table>
  <thead>
    <tr>
      <td colspan="3">TABLE HEADING</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="first" colspan="1">Cell Contents</td>
      <td colspan="1">Cell Contents</td>
      <td colspan="1">Cell Contents</td>
    </tr>
  </tbody>
</table>

,这是我正在使用的 jQuery:

   <script type="text/javascript" language="javascript">
      $(document).ready(function () {
         $("thead").click(function () {
               $(this).next("tbody").slideToggle("slow");
            }
         )
      });
   </script>

I have a table with a thead and tbody sections. I have applied a slideToggle on this successfully, but the animation is broken.

When a user clicks on the thead, I want the contents of the tbody to slide up. Currently what happens is the section simply disappears, without any animation.

Here is the table

<table>
  <thead>
    <tr>
      <td colspan="3">TABLE HEADING</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="first" colspan="1">Cell Contents</td>
      <td colspan="1">Cell Contents</td>
      <td colspan="1">Cell Contents</td>
    </tr>
  </tbody>
</table>

And here is the jQuery I am using:

   <script type="text/javascript" language="javascript">
      $(document).ready(function () {
         $("thead").click(function () {
               $(this).next("tbody").slideToggle("slow");
            }
         )
      });
   </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-12-04 02:59:36

它会消失,因为 通常不会比最高的 td 短,无论您使用 CSS 将其高度设置为多少。

这就是为什么自然高度的 tbody 似乎消失了,而人工额外高度的则似乎一直运行,直到 tr 达到其自然高度。

您可以使用 tbody {display:block;} 来解决这个问题。 查看 jsFiddle 上的拼贴。

但是注意设置表格高度时的效果。

也许,最好的方法是将整个表格包装在一个 div 中,然后slideToggle ,就像这样:

<table class="AbbyNormal">
    <thead><tr><td colspan="3">TABLE HEADING</td></tr></thead>
</table>
<div class="tableWrap">
    <table class="AbbyNormal">
      <tbody>
        <tr>
            <td class="first" colspan="1">Cell Contents</td>
            <td colspan="1">Cell Contents</td>
            <td colspan="1">Cell Contents</td>
        </tr>
      </tbody>
    </table>
</div>

只需确保并将表格宽度固定为相同即可。

在 jsFiddle 上查看它的实际应用。

It disappears because <tbody> normally will get no shorter than the tallest td, no matter what you set its height to with CSS.

This is why the natural-height tbody just seems to disappear, while the one with artificial extra-height appears to run until the tr reached its natural height.

You can kludge around this with tbody {display:block;}. See the kludge at jsFiddle.

But, notice the effect that has when a table height is set.

Probably, the best way is to wrap the whole table in a div and slideToggle that, like so:

<table class="AbbyNormal">
    <thead><tr><td colspan="3">TABLE HEADING</td></tr></thead>
</table>
<div class="tableWrap">
    <table class="AbbyNormal">
      <tbody>
        <tr>
            <td class="first" colspan="1">Cell Contents</td>
            <td colspan="1">Cell Contents</td>
            <td colspan="1">Cell Contents</td>
        </tr>
      </tbody>
    </table>
</div>

Just be sure and fix the table widths the same.

See it in action at jsFiddle.

长安忆 2024-12-04 02:59:36

我认为你应该为 tbody 设置一个高度以使其工作,看看这个小提琴:http://jsfiddle。净/nicolapeluchetti/AsDvb/

css:

tbody{

    height: 1000px;
    background-color: yellow;
}

I think you should set an height to the tbody to make it work, look at this fiddle: http://jsfiddle.net/nicolapeluchetti/AsDvb/

css:

tbody{

    height: 1000px;
    background-color: yellow;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文