Why can I disable the mousedown event, but not its mouseup chain event?

I have two chain mouse events:

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

However, when I try to execute off()them as from another external function, I can only off() mousedown, but not mouseup, whose functionality continues to work.

Can this nested addEventListenerprevent me from disabling its event? (

By the way, it doesn’t matter how I chain: ( $().off().off();) or unchain ( $().off(); $().off();) or combine ( $().off(A B);) or vice versa (AB ↔ BA) elements; off(mousedown)works consistently , but never off(up).

Here is my full JS code:

(The problematic part is the second script, at the end :)

<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>
+4
1

, .
. , , .
"" 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);
  });

/

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

, mousedown mouseup .

page load event listener - textbox

mouseclick .

keydown key and keyup event

, .

after unbind

jsfiddle

+1

Source: https://habr.com/ru/post/1667206/


All Articles