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

查看:31
本文介绍了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.

谢谢!

推荐答案

你可以传递一个 event-map 作为第一个参数:

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天全站免登陆