开发者

Jquery right click, no contextmenu, live and all browser

开发者 https://www.devze.com 2023-02-03 09:08 出处:网络
i need i nice solution to manage the left-& rightclick on a div with a special class (live). It is important for me that the code works on every new browser.

i need i nice solution to manage the left-& rightclick on a div with a special class (live).

It is important for me that the code works on every new browser.

Hope somebody can help me.

My first try ... // the rightclick does not work

HTML

<div id="test_one" class="my_class">Click here</div>
<br /><br />
<div id="test_two" class="my_class">Click here</div>

JS

$(document).ready(function test()
{

    $(".my_class").bind("contextmenu",function(e){ return false; });

    $(".my_class").live('click', function(e)
    {
      if(e.button == 开发者_运维百科0 || e.button == 1)
      {
           alert('L -> '+this.id+'');
      }
      else if(e.button == 2){
           alert('R -> '+this.id+'');
      }
    });


});

example: http://jsfiddle.net/EWXse/

Thanks in advance! Peter


According to the documentation of mousedown the right mouse lick is not detectable by default: http://api.jquery.com/mousedown/ .

How to distinguish between left and right mouse click with jQuery suggests that you use event.which instead of event.button, as .which is normalised to 1,2,3 across all browsers.

For what it is worth: right mouse clicks don't seem to work in Google Chrome on Mac OS X here.

0

精彩评论

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

关注公众号