Javascript 在jQuery中处理多个事件

Javascript 在jQuery中处理多个事件,javascript,jquery,events,Javascript,Jquery,Events,我可否转介: $(document).bind("mousedown", function(){ n = 1; }); $(document).bind("mouseup", function(){ n = 2; }); $(document).bind("mousemove", function(){ n = 3; }); 变成这样: $(document).bind("mousemove mouseup mousedown", function(e)

我可否转介:

$(document).bind("mousedown", function(){
    n = 1;
});  

$(document).bind("mouseup", function(){
    n = 2;
});  

$(document).bind("mousemove", function(){
    n = 3;
});  
变成这样:

$(document).bind("mousemove mouseup mousedown", function(e){
    if (e.event == mousemove){
        n = 3;
    }
});  

添加更多文本,因为stackoverflow说我的帖子主要包含代码

您可以在您的案例中使用
event.type
来确定它是什么事件

$(document).bind("mousemove mouseup mousedown", function(e){
    if (e.type== "mousemove"){
        n = 3;
    }...
});  

在您的情况下,
e
已经是一个
事件
,因此
e

上没有名为event的属性,而不是尝试使用if语句检查事件:

$(document).bind("mousemove mouseup mousedown", function(e){
    if (e.type == mousemove){
        n = 3;
    } else if {
        //some code
    } ...   
});
你可以简单地:

$( document ).bind({
       mousemove: function() {
       n = 3;
    },
       mouseup: function() {
       // Do something on mouseup
    },
       mousedown: function() {
       // Do something on mousedown
    }

});
代码更干净,应该执行得更快

如jQueryAPI文档所示。

请看这篇文章:(几乎相同的问题)一个小小的建议是使用查找来避免if-
n=({mousedown:1,mouseup:2,…})[e.type]或者,另一种选择是将非数字名称保留为字符串(我发现比数字更清楚:>)。@user2246674我不认为他实际上指的是n=1,2,3可能是一个伪代码操作。@PSL您可能是正确的。e.type而不是e.event;)