在 jQuery 中动画删除表行

发布于 2024-08-01 22:37:30 字数 535 浏览 2 评论 0原文

我编写了一些 jQuery 代码,以便在从表中删除行时使用“slideUp”动画。 为了使动画看起来流畅,我用 DIV 标签将行中每个 TD 的内容包裹起来,并在 DIV 上调用 slipUp 函数,在动画完成后删除实际的 TD。 其代码如下:

$("tr[id$=" + rowID + "]").children("td").each(function() {
    $(this).children("div").slideUp(function() {
        $(this).parent().remove();
    });        
});

到目前为止,一切都很好。 但是,我注意到这段代码并没有删除实际的 TR,只是删除了它的内容,因此我添加了以下行来删除 TR:

$("tr[id$=" + rowID + "]").remove();

问题是,在添加上面的行后,动画停止工作。 换句话说,该行只是消失了,没有良好的滑动效果。 有谁知道为什么会这样以及如何解决它?

I have written some jQuery code to use the "slideUp" animation when deleting rows from a table. In order for the animation to appear smooth, I wrapped the contents of each TD in the row with DIV tags, and called the slideUp function on the DIVs, removing the actual TDs upon completion of the animation. The code for that is as follows:

$("tr[id$=" + rowID + "]").children("td").each(function() {
    $(this).children("div").slideUp(function() {
        $(this).parent().remove();
    });        
});

So far, so good. However, I noticed that this code does not remove the actual TR, only its contents, so I added the following line to remove the TR:

$("tr[id$=" + rowID + "]").remove();

The problem is that after I added the line above, the animation quit working. In other words, the row simply disappears without the nice sliding effect. Does anybody know why this is so and how to get around it?

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

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

发布评论

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

评论(2

却一份温柔 2024-08-08 22:37:30

因为您要删除包含所有动画元素的 tr。 不再有元素,不再有动画。 鉴于所有 slideUp 应该花费相同的时间,您可能可以通过从 $(this).parent().remove()< 更改完成回调来逃脱/code> 到 $(this).closest('tr').remove()

Because you're removing the tr that contains all of the animated elements. No more elements, no more animation. Given that all of the slideUps should be taking the same amount of time, you can probably get away with changing the finish callback from $(this).parent().remove() to $(this).closest('tr').remove().

残龙傲雪 2024-08-08 22:37:30

我写了一个 jQuery 插件,可以让你做到这一点。 您可以添加和删除行,并且不需要用 div 或类似的东西包装您的数据。 请访问 http:// www.fletchzone.com/post/jQuery-Unobtrusively-Animated-Add-and-Remove-Table-Rows.aspx

最好,

Fletch

I wrote a jQuery plugin that lets you do this. You can add and remove rows and it doesn't require wrapping your data with a div or anything like that. Check it out at http://www.fletchzone.com/post/jQuery-Unobtrusively-Animated-Add-and-Remove-Table-Rows.aspx

Best,

Fletch

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