gpt4 book ai didi

javascript - 绑定(bind)多个事件并在所有事件触发时运行函数! JS、Jquery

转载 作者:行者123 更新时间:2023-12-02 17:41:26 24 4
gpt4 key购买 nike

我想将两个鼠标事件绑定(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com