jQuery - 将.live()转换为.on() [英] jQuery - convert .live() to .on()

查看:70
本文介绍了jQuery - 将.live()转换为.on()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将这个旧的jQuery代码组合到v1.7 .on()

How do I go about combining this old jQuery code into the v1.7 .on()?

v1.3 .live()

    $('#results tbody tr').live({
    mouseenter:
       function () { $(this).find('.popup').show(); },
    mouseleave:
       function () { $(this).find('.popup').hide(); }
    });

v1.7 .on()

$('#results tbody').on('mouseenter', 'tr', function () {
    $(this).find('.popup').show();
});
$('#results tbody').on('mouseleave', 'tr', function () {
    $(this).find('.popup').hide();
});

我想将两个事件处理程序传递给一个 .on()打电话,但保持辉煌的事件委托 .on()允许我这样做。

I want to pass both event handlers to one .on() call, but keep the brilliant event delegation .on() allows me to do.

谢谢!

推荐答案

您可以将事件图作为第一个参数传递:

You can pass an event-map as the first parameter:

$('#results tbody').on({
    'mouseenter' : function () {
        $(this).find('.popup').show();
     },
    'mouseleave' : function () {
        $(this).find('.popup').hide();
    }
}, 'tr');

jQuery文档


.on(events-map [,selector] [,data])

events-map 一个映射,其中字符串键表示一个或多个空格分隔的事件类型和
可选命名空间,值表示要处理的函数
要求举办活动。

.on( events-map [, selector] [, data] ),
events-map A map in which the string keys represent one or more space-separated event types and optional namespaces, and the values represent a handler function to be called for the event(s).

这篇关于jQuery - 将.live()转换为.on()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆