Cancel click event in the mouseup event handler

前端 未结 14 2416
梦毁少年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:26
    $(document).mouseup(function(event){
    
        event.preventDefault(); // this prevents only a default action but previously assigned listeners will be called
        event.stopImmediatePropagation() // if there are  others listeners which that shouldn't call 
    }
    
    0 讨论(0)
  • 2020-12-09 15:28

    There is a solution!

    This approach works for me very well (at least in chrome):

    on mousedown I add a class to the element that is currently being moved and on mouseup I remove the class.

    All that class does is sets pointer-events:none

    Somehow this makes it work and click event is not fired.

    0 讨论(0)
  • 2020-12-09 15:32

    If you wish suppress the click event you have add the statement event.preventDefault() in the click event handler only and not the mouseup event handler. Use the below code you will get it working.

    <div id="test">test</div>
    <script>
    $("#test").mouseup (function (e) {
      var a = 1;
    
    });
    $("#test").click (function (e) {
     e.preventDefault();
      var a = 2;
    });
    

    Hope this solves your problem.

    0 讨论(0)
  • 2020-12-09 15:35

    It might be possible but I'm not sure if you can handle this kind of evt management with jQuery. This code example should not be far away from your expectation or at least give you a direction.

    function AddEvent(id, evt_type, ma_fonction, phase) {
        var oElt = document.getElementById(id);
        // modèle W3C mode bubbling
        if( oElt.addEventListener ) {
            oElt.addEventListener(evt_type, ma_fonction, phase);
        // modèle MSIE
        } else if( oElt.attachEvent ) {
            oElt.attachEvent('on'+evt_type, ma_fonction);
        }
        return false;
    }
    
    function DelEvent(id, evt_type, ma_fonction, phase) {
        var oElt = document.getElementById(id);
        // modèle W3C mode bubbling
        if( oElt.removeEventListener ) {
            oElt.removeEventListener(evt_type, ma_fonction, phase);
        // modèle MSIE
        } else if( oElt.detachEvent ) {
            oElt.detachEvent('on'+evt_type, ma_fonction);
        }
        return false;
    }
    
        var mon_id = 'test';
        var le_type_evt = "mouseup";
        var flux_evt = false; // prevent bubbling
        var action_du_gestionnaire = function(e) {
            alert('evt mouseup on tag <div>');
    
            // 1ère méthode : DOM Lev 2
            // W3C
                if ( e.target )
                    e.target.removeEventListener(le_type_evt, arguments.callee, flux_evt);
            // MSIE
                else if ( e.srcElement )
                    e.srcElement.detachEvent('on'+le_type_evt, arguments.callee);
    
            // 2ème méthode DOM Lev2
            // DelEvent(mon_id, le_type_evt, action_du_gestionnaire, flux_evt);
        };
    
    
        AddEvent(mon_id, le_type_evt, action_du_gestionnaire, flux_evt);
    
    0 讨论(0)
  • 2020-12-09 15:36

    the solution that i use is the following:

    var disable_click = false;
    
    function click_function(event){
        if (!disable_click){
            // your code
        }
    }
    
    function mouse_down_function(event){
        disable_click = false; // will enable the click everytime you click
        // your code
    }
    
    function mouse_up_function(event){
        // your code
    }
    
    function mouse_drag_function(event){
        disable_click = true; // this will disable the click only when dragging after clicking
       // your code
    }
    

    attach each function to the appropriate event according to the name !

    0 讨论(0)
  • 2020-12-09 15:37

    I had the same problem and didn't found a solution either. But I came up with a hack that seems to work.

    Since the onMouseUp handler doesn't seem to be able to cancel the click on a link with preventDefault or stopEvent or anything, we need to make the link cancel itself. This can be done by writing an onclick attribute which returns false to the a-tag when the drag begins, and removing it when the drag ends.

    And since the onDragEnd or onMouseUp handlers are run before the click is interpreted by the browser, we need to do some checking where the drag ends and which link is clicked and so on. If it ends outside the dragged link (we dragged the link so that the cursor isn't on the link anymore), we remove the onclick handler in the onDragEnd; but if it ends where the cursor is on the dragged link (a click would be initiated), we let the onclick-handler remove itself. Complicated enough, right?

    NOT COMPLETE CODE, but just to show you the idea:

    // event handler that runs when the drag is initiated
    function onDragStart (args) {
      // get the dragged element
      // do some checking if it's a link etc
      // store it in global var or smth
    
      // write the onclick handler to link element
      linkElement.writeAttribute('onclick', 'removeClickHandler(this); return false;');
    }
    
    // run from the onclick handler in the a-tag when the onMouseUp occurs on the link
    function removeClickHandler (element) {
      // remove click handler from self
      element.writeAttribute('onclick', null);
    }
    
    // event handler that runs when the drag ends
    function onDragEnds (args) {
      // get the target element
    
      // check that it's not the a-tag which we're dragging,
      // since we want the onclick handler to take care of that case
      if (targetElement !== linkElement) {
        // remove the onclick handler
        linkElement.writeAttribute('onclick', null);
      }
    }
    

    I hope this gives you an idea of how this can be accomplished. As I said, this is not a complete solution, just explaining the concept.

    0 讨论(0)
提交回复
热议问题