开发者

Bind to right-click with jQuery?

开发者 https://www.devze.com 2023-04-11 15:47 出处:网络
I want to bind a function to a right 开发者_StackOverflowclick. Is this possible with jQuery UI?though not listed on http://api.jquery.com/bind/, the \'contextmenu\' event seems to work

I want to bind a function to a right 开发者_StackOverflowclick. Is this possible with jQuery UI?


though not listed on http://api.jquery.com/bind/, the 'contextmenu' event seems to work

$('.rightclickable').bind('contextmenu', function() {
    // right-click!
});


Not directly, but you can check which mouse button was pressed in a normal mousedown event handler, with the which property of the event object:

$("#someElem").mousedown(function(e) {
    if(e.which == 3) {
        //Right click!
    }
});

Here's a working example of the above.


Try

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        //your
    });
});


$(document).bind('contextmenu',function(){
    return false;
});
$.fn.extend({
    "rightClick": function(fn){
        $(this).mousedown(function(e){
            if (3 == e.which) {
                fn();
            }
        });
    }
});
$(function(){
    $('selector').rightClick(function(){
        // defined your right click event here!
    });
}); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号