Handle multiple events in jQuery
Can I turn this:
$(document).bind("mousedown", function(){
n = 1;
});
$(document).bind("mouseup", function(){
n = 2;
});
$(document).bind("mousemove", function(){
n = 3;
});
Into something like this:
$(document).bind("mousemove mouseup mousedown", function(e){
if (e.event == mousemove){
n = 3;
}
});
Adding more text because stackoverflow says my post contains mostly code
You can use event.type
in your case e.type
to determine what event it is.
$(document).bind("mousemove mouseup mousedown", function(e){
if (e.type== "mousemove"){
n = 3;
}...
});
In your case e
is already an event
so there is no property called event on e
Well instead of trying to do this, with if statements to check the event:
$(document).bind("mousemove mouseup mousedown", function(e){
if (e.type == mousemove){
n = 3;
} else if {
//some code
} ...
});
You could simply:
$( document ).bind({
mousemove: function() {
n = 3;
},
mouseup: function() {
// Do something on mouseup
},
mousedown: function() {
// Do something on mousedown
}
});
Code is much cleaner and it should perform faster.
As shown on the JQuery API Documentation. http://api.jquery.com/bind/#multiple-events
链接地址: http://www.djcxy.com/p/96928.html上一篇: 多个事件触发相同的功能,但具有不同的元素
下一篇: 在jQuery中处理多个事件