我有这个代码
$("td").hover(function(){
$(this).find('a.btn').show();
}, function(){
$(this).find('a.btn').hide();
})
如何使用 on
为新的 dom 元素转换此函数
最佳答案
$("#mytable").on('hover', 'td', function(){
$(this).find('a.btn').show();
}, function(){
$(this).find('a.btn').hide();
});
但是使用 'hover'
伪事件代替传递 'mouseenter mouseleave'
已被弃用,因此您应该真正使用 mouseenter
和 mouseleave
直接。
$("#mytable").on('mouseenter', 'td', function(){
$(this).find('a.btn').show();
})
.on('mouseleave', 'td', function(){
$(this).find('a.btn').hide();
});
或者像这样:
$("#mytable").on({'mouseenter': function(){
$(this).find('a.btn').show();
}, 'mouseleave': function(){
$(this).find('a.btn').hide();
}}, 'td');
或者像这样更短:
$("#mytable").on('mouseenter mouseleave', 'td', function(e){
$(this).find('a.btn').toggle(e.type === 'mouseenter');
});
关于javascript - 如何在jquery中使用on转换悬停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13004722/