我想将多个事件附加到单个元素。但我想为每个事件附加不同的数据。在jQuery中附加多个事件的方法如下:
$("div.test").on({
click: this.clickEventCalled,
mouseenter: this.mouseEnterEventCalled
mouseleave: this.mouseLeaveEventCalled
});
但我希望能够做到这样的事情:
$("div.test").on("click", { name: "Karl" }, clickEventCalled);
我想知道是否有更优雅的方式来做这件事:
var self = this;
$("div.test").on({
click: function(event){
self.clickEventCalled(event,{name:"Karl"})
},
mouseenter: function(event){
self.mouseEnterEventCalled(event,{name:"Ben"})
},
mouseleave: function(event){
self.mouseLeaveEventCalled(event,{name:"Ken"})
}
});
答案 0 :(得分:0)
你可以这样做:
$("div.test").on("click", { name: "Karl" }, clickEventCalled)
.on("mouseenter", { name: "Ben" }, mouseEnterEventCalled)
.on("mouseleave", { name: "Ken" }, mouseLeaveEventCalled);
});