试图弄清楚如何将Jquery .on()方法与具有多个与之关联的事件的特定选择器一起使用.我之前使用过.live()方法,但不太确定如何用.on()完成同样的壮举.请参阅下面的代码:
$("table.planning_grid td").live({
mouseenter:function(){
$(this).parent("tr").find("a.delete").show();
},
mouseleave:function(){
$(this).parent("tr").find("a.delete").hide();
},
click:function(){
//do something else.
}
});
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过调用以下方式分配多个事件:
$("table.planning_grid td").on({
mouseenter:function(){ //see above
},
mouseleave:function(){ //see above
}
click:function(){ //etc
}
});
Run Code Online (Sandbox Code Playgroud)
但我相信正确使用.on()会是这样的:
$("table.planning_grid").on('mouseenter','td',function(){});
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?或者这里的最佳做法是什么?我尝试了下面的代码,但没有骰子.
$("table.planning_grid").on('td',{
mouseenter: function(){ /* event1 */ },
mouseleave: function(){ /* event2 */ },
click: function(){ /* event3 */ }
});
Run Code Online (Sandbox Code Playgroud)
提前致谢!