$something.on('mousedown touchstart mousemove', function(e){
// everything do something
if('mousedown') // do something
if('touchstart') // do something
if('mousemove') // do something
});
有可能做这样的事情吗?正确的方法是什么?
$something.on('mousedown touchstart mousemove', function(e){
// everything do something
if('mousedown') // do something
if('touchstart') // do something
if('mousemove') // do something
});
有可能做这样的事情吗?正确的方法是什么?
您可以switch
用于event.type
:
$something.on("mousedown touchstart mousemove", function(e) {
switch (e.type) {
case "mousedown":
// do something
break;
case "touchstart":
// do something
break;
case "mousemove":
// do something
}
});
另一种方法是使用内部方法创建一个处理函数来匹配事件:
$('div').on('mouseenter click', evtHandler)
function evtHandler(evt, el) {
var _self=this, $el=$(this);
var handler = {
click: function() {
$el.text('Div was clicked on')
},
mouseenter: function() {
$el.css('background','#EAEAEA')
}
};
handler[evt.type]();
}
演示:http: //jsfiddle.net/ufYKF/