1
$something.on('mousedown touchstart mousemove', function(e){
   // everything do something
   if('mousedown') // do something

   if('touchstart') // do something

   if('mousemove') // do something
});

有可能做这样的事情吗?正确的方法是什么?

4

2 回答 2

2

您可以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
    }
});
于 2012-12-02T11:51:22.123 回答
0

另一种方法是使用内部方法创建一个处理函数来匹配事件:

$('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/

于 2012-12-02T12:36:44.737 回答