How to distinguish between left and right mouse click with jQuery

后端 未结 17 2696
独厮守ぢ
独厮守ぢ 2020-11-21 22:36

How do you obtain the clicked mouse button using jQuery?

$(\'div\').bind(\'click\', function(){
    alert(\'clicked\');
});

this is trigger

17条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 22:50

    $.fn.rightclick = function(func){
        $(this).mousedown(function(event){
            if(event.button == 2) {
                var oncontextmenu = document.oncontextmenu;
                document.oncontextmenu = function(){return false;};
                setTimeout(function(){document.oncontextmenu = oncontextmenu;},300);
                func(event);
                return false;
            }
        });
    };
    
    $('.item').rightclick(function(e){ 
        alert("item");
    }); 
    

提交回复
热议问题