JS: How to prevent the average click processing from default?

This seems like a ridiculous easy problem, but it seems to be more complicated ...

I want to prevent the default processing of the middle click . I have created and introduced JSFiddle stopPropagation, stopImmediatePropagation, preventDefaultand return false- like so:

$(document).on("mousedown", "a", function(e)
{    
    console.log("\nprevent mousedown...");
    e.stopPropagation();
    e.stopImmediatePropagation();
    e.preventDefault();
    console.log("...mousedown prevented");
    return false;
});

But the middle click starts. BTW he is fired when I release the middle button. Here's JSFiddle: http://jsfiddle.net/Gq4p9/4/

Tested on Chrome 29, Firefox 23 and IE11.

I hope one of you finds out why this script does not prevent default processing.

+4
source share
3 answers

, , jQuery

$(document).on ("click", $("a"), function (e) { ...

API , selector .

Fiddle


javascript clickListener.

link.addEventListener ("click", function (e) {
  if (e.which === 2) 
      e.preventDefault();
})

Heres a Fiddle

+3

( : , ). , , , click, mousedown .

, IE8 , click event which 0 , . jQuery, jquery.whichclick, : leftclick, rightclick, middleclick anyclick - event.which stopPropagation, stopImmediatePropagation preventDefault click.

:

$( document ).on( 'middleclick', function( e ){
    e.preventDefault();
} );

:

$( document ).on( 'anyclick', function( e ){
    if( e.which === 2 ){
        e.preventDefault();
    }
    else {
        // Other conditions you may be looking for...
    }
} );

IE, - , !

+2

, , :

    if( e.which == 2 ) { 
        // prevent default behaviour
    }

0

All Articles