0

当您有以下代码时,如何确定正在发生的操作或事件

$('#mydiv').on('click mousedown mousemove', function(event) { //<--multiple event
    if (action is mousedown) {

    }
    else if(action is mousemove){

    }
    else{
        //mouse click
    }
});

欢迎使用 jsFiddle 示例。

4

3 回答 3

3

event.type将事件类型作为字符串获取:

$('#mydiv').on('click mousedown mousemove', function(event) {
    switch(event.type) {
        case 'click':
                //do something on click
            break;
        case 'mousedown':
                //do something on mousedown
            break;
        case 'mousemove':
                //do something on mousemove 
            break;

       .....etc.
    }

});

你也可以这样做:

$('#mydiv').on(
   click: function() {
        //do something on click
   },
   mousedown: function() {
       //do something on mousedown
   },
   mousemove: function() {
      //do something on mousemove
   }
});
于 2012-12-06T23:21:36.653 回答
1

您可以访问它与哪个事件:

event.type

你的代码可以写成:

$('#mydiv').on('click mousedown mousemove', function(event) {
    if (event.type == 'mousedown') {

    }
    else if (event.type == 'mousemove'){

    }
    else{
        //mouse click
    }
});

参考:event.type

于 2012-12-06T23:22:02.670 回答
0

你可以捕捉到event.type:

$('#mydiv').on('click mousedown mousemove', function(event) { //<--multiple event
    console.log(event.type);
});​

http://jsfiddle.net/ra9r5/

于 2012-12-06T23:23:44.090 回答