jQuery SlideToggle 在带有 thead 和 tbody 元素的表格上。没有动画。
我有一张带有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它会消失,因为
通常不会比最高的
td
短,无论您使用 CSS 将其高度设置为多少。这就是为什么自然高度的
tbody
似乎消失了,而人工额外高度的则似乎一直运行,直到tr
达到其自然高度。您可以使用
tbody {display:block;}
来解决这个问题。 查看 jsFiddle 上的拼贴。但是,注意设置表格高度时的效果。
也许,最好的方法是将整个表格包装在一个 div 中,然后
slideToggle
,就像这样:只需确保并将表格宽度固定为相同即可。
在 jsFiddle 上查看它的实际应用。
It disappears because
<tbody>
normally will get no shorter than the tallesttd
, 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 thetr
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:Just be sure and fix the table widths the same.
See it in action at jsFiddle.
我认为你应该为 tbody 设置一个高度以使其工作,看看这个小提琴:http://jsfiddle。净/nicolapeluchetti/AsDvb/
css:
I think you should set an height to the tbody to make it work, look at this fiddle: http://jsfiddle.net/nicolapeluchetti/AsDvb/
css: