我有这个代码
$("td").hover(function(){
$(this).find('a.btn').show();
}, function(){
$(this).find('a.btn').hide();
})如何使用on将此函数转换为新的dom元素
发布于 2012-10-22 11:46:53
$("#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');
});发布于 2012-10-22 11:48:17
我会这样做:
$('td').on({
mouseenter: function() { $(this).find('a.btn').show() }
mouseleave: function() { $(this).find('a.btn').hide() }
})编辑:你的问题并不清楚你是否需要委派,在这种情况下,请查看其他答案。
https://stackoverflow.com/questions/13004722
复制相似问题