如何将这个旧的 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');
.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/