【发布时间】:2014-05-15 00:34:45
【问题描述】:
我的问题与空间和可读性有关,但我开始了。
我需要使用事件委托来让动态添加的元素与我的 jQuery 函数一起使用。但是,我希望结合两种处理事件的方式。基本上我想要这个:
$("div").on({
mouseenter: function() {
console.log( "hovered over a div" );
},
mouseleave: function() {
console.log( "mouse left a div" );
},
click: function() {
console.log( "clicked on a div" );
}
});
但我想使用事件委托并为每个事件设置选择器:
$("ul").on("click", "li", function() {
console.log( "Something in a <ul> was clicked, and we detected that it was an <li> element.");
});
结论:使用第一个示例时是否可以进行事件委托? 我想要的示例:
$("div").on({
mouseenter, 'div': function() {
console.log( "hovered over a div" );
},
mouseleave, 'tr, td, div': function() {
console.log( "mouse left a div" );
},
click, 'td div': function() {
console.log( "clicked on a div" );
}
});
【问题讨论】:
-
Edit 修复了一些大写字母和代码上的一些缩进。猜猜它让整个事情看起来更干净了,thnx ;)
标签: jquery events delegation handlers