gpt4 book ai didi

jquery - 为什么我可以关闭 mousedown 事件,但不能关闭其链接的 mouseup 事件?

转载 作者:行者123 更新时间:2023-12-01 00:26:28 29 4
gpt4 key购买 nike

我有两个链接的鼠标事件:

$('body > form').on("mousedown", function(e){ 
//Do stuff
}).on("mouseup", function(){
/*More stuff, including
window.addEventListener(...
*/
});

但是,当我尝试从另一个外部函数 off() 它们时,我只能 off() mousedown,但不能mouseup,其功能仍然有效。

嵌套的 addEventListener 是否会阻止我关闭其事件? (

顺便说一下,我如何做并不重要链: ($().off().off();),或解链 ($().off(); $().off(); ),或组合 ($().off(A B);) 或反转 (A B <-> B A) 元素;始终如一,off(mousedown) 有效,但永远不会 off(up)

这是我的完整 JS 代码:

(有问题的部分是最后的第二个脚本:)

<script>
function comment() {

$('body > form').on("mousedown.markerPlacer", function(e){
//Place the cursor marker on the screen

var newComment2 = $('<div id="newComm" class="marker" class="deg45" &uarr;</div>');
$('form').append(newComment2);

}).on("mouseup", function(){
//Now use the Q-key to rotate the marker.

window.addEventListener("keydown", extraKey, false);
window.addEventListener("keyup", keysReleased2, false);

function extraKey(e) {
var deg = e.keyCode;
if (deg == 81) { //81 is the keycode, which codes for Q
$('#newComm').attr('class', 'marker').addClass('deg0');
} else {
return false;
};
e.preventDefault();
};
function keysReleased2(e) {
e.keyCode = false;
};
});
};
</script>

<script>
function dismissComment() {
$('body > form').off("mousedown mouseup");
}
</script>

最佳答案

出于好奇,我编写了一个类似的链接 mousedown 和 mouseup,如代码中所示。
我通过单击按钮来解除它们的绑定(bind)。它似乎工作正常,但我无法重现您的问题。
它能够“关闭” mousedown 和 mouseup 事件监听器。

$("#testMe")
.on("mousedown", function(e) {
appendText(getButton(e.button) + " | " + $(this).attr("id") + " | " + "mouseDOWN | " + new Date());
})
.on("mouseup", function(e) {
appendText(getButton(e.button) + " | " + $(this).attr("id") + " | " + "mouseUP | " + new Date());

$(document).off("keydown keyup");
$(document).on("keydown", keydown_pressed);
$(document).on("keyup", keyup_pressed);
});

解除绑定(bind)/“关闭”代码

$("#unbind").click(function(e) {
$("#testMe").off("mousedown mouseup");
$(document).off("keydown keyup");
});

页面加载时,mousedown 和 mouseup 会附加到输入文本框。

page load event listener - textbox

鼠标单击文本框之后,添加窗口 keydown 和 keyup 事件监听器。

document keydown and keyup event

在我“关闭”或取消绑定(bind)之后,所有事件监听器都将被删除。

after unbind

jsfiddle link

关于jquery - 为什么我可以关闭 mousedown 事件,但不能关闭其链接的 mouseup 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732071/

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