尝试弄清楚如何将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.
}
});
我知道我可以通过调用以下方法来分配多个事件:
$("table.planning_grid td").on({
mouseenter:function(){ //see above
},
mouseleave:function(){ //see above
}
click:function(){ //etc
}
});
但我相信.on()的正确用法应该是这样的:
$("table.planning_grid").on('mouseenter','td',function(){});
有没有办法做到这一点?或者,这里的最佳实践是什么?我尝试了下面的代码,但没有结果。
$("table.planning_grid").on('td',{
mouseenter: function(){ /* event1 */ },
mouseleave: function(){ /* event2 */ },
click: function(){ /* event3 */ }
});
提前感谢!
发布于 2011-12-23 02:18:15
那是the other way around。你应该这样写:
$("table.planning_grid").on({
mouseenter: function() {
// Handle mouseenter...
},
mouseleave: function() {
// Handle mouseleave...
},
click: function() {
// Handle click...
}
}, "td");
发布于 2014-06-24 23:46:52
此外,如果将多个事件处理程序附加到执行相同函数的同一选择器,则可以使用
$('table.planning_grid').on('mouseenter mouseleave', function() {
//JS Code
});
发布于 2017-07-26 20:40:18
如果您想在不同的事件上使用相同的函数,可以使用以下代码块
$('input').on('keyup blur focus', function () {
//function block
})
https://stackoverflow.com/questions/8608145
复制相似问题