我想将两个鼠标事件绑定(bind)到一个函数(mousedown 和 moucemove)。但我只想在两个事件都被触发时才运行该函数。
这会将每个事件绑定(bind)到函数:(这不是我想要的)
$("#someid").bind("mousedown mousemove", function (event) { someFunction(); });
我可以做到这一点并且它有效:
$("#someid").bind("mousedown", function (event) {
someFunction();
$("#someid").bind("mousemove", function (event) {
someFunction();
});
});
$("#someid").bind("mouseup", function (event) {
$("#someid").unbind("mousemove");
});
有更好、更快的方法吗???
最佳答案
仅绑定(bind)到mousemove事件。如果在移动时按下鼠标左键,则 event.which 将为 1。
$(document).on('mousemove', function(e) {
if (e.which == 1) {
//do some stuff
}
});
关于javascript - 绑定(bind)多个事件并在所有事件触发时运行函数! JS、Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173023/