JQuery .on() 方法用于一个选择器上的多个事件处理程序
JQuery .on() 方法用于一个选择器上的多个事件处理程序
尝试弄清如何使用带有多个事件的特定选择器的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(){ //做其他事情。 } });
我知道我可以通过调用以下方式来分配多个事件:
$("table.planning_grid td").on({ mouseenter:function(){ //见上面 }, mouseleave:function(){ //见上面 } click:function(){ //等等 } });
但我相信正确使用.on()的方式应该是这样的:
$("table.planning_grid").on('mouseenter','td',function(){});
有办法实现这个吗?或者在这里有什么最佳实践?我尝试了下面的代码,但没有成功。
$("table.planning_grid").on('td',{ mouseenter: function(){ /* event1 */ }, mouseleave: function(){ /* event2 */ }, click: function(){ /* event3 */ } });