使用 jQuery 1.3.2 进行事件委托

发布于 2024-10-08 06:48:12 字数 637 浏览 0 评论 0原文

我现在一直在使用 jQuery 1.3.2,而且我才刚刚开始了解一般的事件委托。但我似乎无法让任何类型的事件委托来处理此代码。我有一个带有“聊天”类的 ul,当将鼠标悬停在其中一个 li 上时,应该创建一个新的跨度,然后滑出,然后在鼠标离开 li 时滑回。

此代码有效,但我想使用事件委托:

$('ul.chat li').hover(
    function() {
        $(this).append($('<span class="join">Join Conversation</span>'));
        setTimeout(function() {
            $('.join').animate({'width': '150px'}, 400);
        },500);
    },
    function() {
        $('.join').animate({'width': '0px'}, 200, function(){
            $(this).remove();                                             
        }); 
    }
);

有人可以告诉我如何通过事件委托实现相同的结果吗? 谢谢。

I'm stuck using jQuery 1.3.2 at the moment and I'm just beginning to understand event delegation in general. But I can't seem to get any sort of event delegation to work on this code. I have a ul with a class 'chat' which when hovering over one of it's li's should create a new span which then slides out, then slides back in when the mouse leaves the li.

This code works, but I want to use event delegation:

$('ul.chat li').hover(
    function() {
        $(this).append($('<span class="join">Join Conversation</span>'));
        setTimeout(function() {
            $('.join').animate({'width': '150px'}, 400);
        },500);
    },
    function() {
        $('.join').animate({'width': '0px'}, 200, function(){
            $(this).remove();                                             
        }); 
    }
);

Can someone show me how to achieve the same result, but with event delegation?
Thanks.

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

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

发布评论

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

评论(1

痞味浪人 2024-10-15 06:48:12

更新:您似乎并不希望为添加的项目进行事件委托,但更多的是因为您将使用非常大的结果集。

在 Google 上快速搜索(“事件委托 jquery 1.3.2”)得出 这个


您可以通过 jQuery.live() 事件获得“事件委托” 。

您必须将其分成两个绑定,如下所示:

$('ul.chat li').live('mouseover',
    function() {
        $(this).append($('<span class="join">Join Conversation</span>'));
        setTimeout(function() {
            $('.join').animate({'width': '150px'}, 400);
        },500);
    }
).live('mouseout',
    function() {
        $('.join').animate({'width': '0px'}, 200, function(){
            $(this).remove();                                             
        }); 
    }
);

UPDATE: Doesn't seem like you want event delegation for added items, but more because you'll be working with a very large result set.

A quick search on Google ('event delegation jquery 1.3.2') came up with this.


You can get "event delegation" with the jQuery.live() event.

You'll have to split that into two bindings, like so:

$('ul.chat li').live('mouseover',
    function() {
        $(this).append($('<span class="join">Join Conversation</span>'));
        setTimeout(function() {
            $('.join').animate({'width': '150px'}, 400);
        },500);
    }
).live('mouseout',
    function() {
        $('.join').animate({'width': '0px'}, 200, function(){
            $(this).remove();                                             
        }); 
    }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文