jquery - 将 .live() 转换为 .on()

标签 jquery event-handling jquery-events

如何将这个旧的 jQuery 代码合并到 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() 调用,但保留出色的事件委托(delegate) .on() 允许我这样做。

最佳答案

您可以传递事件映射作为第一个参数:

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

jQuery documentation :

.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(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10636932/

相关文章:

javascript - 如何向此 url 附加参数?

c# - 事件处理程序快速命中多次似乎为 "short-circuit"

multithreading - BackgroundWorker 中的异步错误处理

javascript - jQuery on() 与其前身的性能对比 [live()、bind()、delegate()]

javascript - 洗牌函数随机化字母

javascript - 从 jQuery 自定义触发器/绑定(bind)事件返回数据

javascript - 如何检查每个输入属性(如果类型为 radio 并已选中)

php - 使用 json 和 ajax 自动完成 JQuery UI

javascript - 向 slider 添加元素符号,该元素符号会随右侧幻灯片自动更改

jquery 将悬停事件处理程序附加到嵌入的 svg 文件中的路径