淡出并删除表格行

发布于 2024-12-02 03:15:39 字数 612 浏览 1 评论 0原文

我知道这个问题之前已经被问过,但我似乎遇到了与之前解决的问题不同的问题。我有一个表,我希望每一行都有一个删除链接,该链接会淡出表行,然后从 DOM 中删除表行。我的第一个问题是我无法让 jQuery fadeOut 效果在表行上工作,并且发现您必须在行的 td 元素上实际调用 fadeOut。所以,这是我的 jJavascript:

$('span.deleteItem').live('click', function() {  
    $(this).closest('tr').find('td').fadeOut('fast', 
        function(){ 
            $(this).parents('tr:first').remove();                    
        });    

    return false;
});

span 元素位于 td 内,因此当单击它时我会找到最接近的 tr 元素,然后将 fadeOut 函数放在其每个 td 元素上。这很好用。

问题在于,在回调函数中,“this”实际上引用的是 window 元素,而不是隐藏的单个 td 元素。根据我的理解,“this”应该引用淡出的元素。

有什么想法吗?

I know this question has been asked before but I seem to have a different problem than has been addressed before. I have a table and I would like each row to have a delete link that fades the table row out and then removes the table row from the DOM. My first problem was that I couldn't get the jQuery fadeOut effect to work on table rows and found that you have to actually call fadeOut on the row's td elements. So, here is my jJavascript:

$('span.deleteItem').live('click', function() {  
    $(this).closest('tr').find('td').fadeOut('fast', 
        function(){ 
            $(this).parents('tr:first').remove();                    
        });    

    return false;
});

The span element lives inside a td so I find the closest tr element when it is clicked, and then fall the fadeOut function on each of its td elements. This works great.

The problem is that in the callback function, 'this' is actually referencing the window element not the individual td element that was hidden. From my understanding 'this' was supposed to reference the element that was faded out.

Any ideas?

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

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

发布评论

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

评论(2

吲‖鸣 2024-12-09 03:15:39

获取“this”引用并将其传递:

$('span.deleteItem').live('click', function() {  
    var here = this;
    $(this).closest('tr').find('td').fadeOut('fast', 
        function(here){ 
            $(here).parents('tr:first').remove();                    
        });    

    return false;
});

Grab the "this" reference and pass it on:

$('span.deleteItem').live('click', function() {  
    var here = this;
    $(this).closest('tr').find('td').fadeOut('fast', 
        function(here){ 
            $(here).parents('tr:first').remove();                    
        });    

    return false;
});
柳若烟 2024-12-09 03:15:39

我认为这就是您正在寻找的:

$('span.deleteItem').live('click', function() { 
    var tableRow = $(this).closest('tr');
    tableRow.find('td').fadeOut('fast', 
        function(){ 
            tableRow.remove();                    
        }
    );
});

编辑:我认为 Opatut 是正确的,如他的 jsFiddle

I think this is what you are looking for:

$('span.deleteItem').live('click', function() { 
    var tableRow = $(this).closest('tr');
    tableRow.find('td').fadeOut('fast', 
        function(){ 
            tableRow.remove();                    
        }
    );
});

EDIT: I think Opatut is right, as show in his jsFiddle.

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