简体   繁体   中英

How to change mouse left click and right click option?

I have some set of HTML files named in sequence. Is it possible to assign mouse right click to next html page and mouse left click to previous html page and how to do this?

This is how we handles the mouse click..

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left mouse button pressed');
            //code to navigate to left page
            break;

        case 2:
            alert('Right mouse button pressed');
           //code to navigate to right page
            break;
        default:
            alert('Mouse is not good');
    }
});
$(function(){
    $(document).mousedown(function(event) {
    switch (event.which) {
        case 1:
            window.location.href = "http://stackoverflow.com" // here url prev. page
            break;
        case 3:
            window.location.href = "http://www.google.com" // here url next. page
            break;
        default:
            break;
    }
    });
  })

And don't forgot add jquery library.

You can also do this with some simple Javascript.

<script type='text/javascript'>
function right(e){
    //Write code to move you to next HTML page
}

<canvas style='width: 100px; height: 100px; border: 1px solid #000000;' oncontextmenu='right(event); return false;'>
     //Everything between here's right click is overridden.
</canvas>

This is the traditional way to override left and right clicks. In the code I'm preventing event propagation of the right-click also so the context menu won't display.

JSFiddle

window.onclick = leftClick
window.oncontextmenu = function (event) {
    event = event || window.event;
    if (event.stopPropagation)
        event.stopPropagation();

    rightClick();

    return false;
}

function leftClick(event) {
    alert('left click');
    window.location.href = "http://www.google.com";
}

function rightClick(event) {
    alert('right click');
    window.location.href = "http://images.google.com";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM