Cancel click event in the mouseup event handler

前端 未结 14 2436
梦毁少年i
梦毁少年i 2020-12-09 14:53

Writing some drag&drop code, I\'d like to cancel the click events in my mouseup handler. I figured preventing default should do the trick, but the click event is still f

14条回答
  •  一向
    一向 (楼主)
    2020-12-09 15:25

    $(document).mouseup(function(event){ // make sure to set the event parameter
        event.preventDefault(); // prevent default, like you said
    });
    

    The important thing to note is the event parameter.

    EDIT: You want to cancel the drag?

    The way I know to do this is to either use bind() (for older jQuery versions) or on() (for jQuery 1.7+) to attach the mousedown event, then use unbind() or off() respectively to detach it.

    $(document)
        .on("mousedown", function(){...})
        .on("mouseup", function(){
            $(document).off("mousedown");
        });
    

提交回复
热议问题