开发者

Right click event (onclick) in Chrome, Opera and IE

In Firefox I used the document.onclick event and then c开发者_运维技巧hecked if it was a right click, and if I right clicked everything went as expected, but in Chrome, Opera and IE8, if I right click the document.onclick doesn't fire.

I want to have a custom context menu for img elements. How do I go about this?


The right-click invokes the context menu within most standard browsers; therefore, you can use the "oncontextmenu" listener to handle right-click events. The listener should return false if you do not want it to display the standard browser context menu after invoking your JS code.

Here's some sample html that handles left and right clicks on an image.

<html>
<head>
    <script type="text/javascript">
        function handleRightClick() {
            alert("Got right click!");
        };

        function handleLeftClick() {
            alert("Got left click!");
        };
    </script
</head>
<body>
    <img src="http://thefuturebuzz.com/pics/the-matrix.jpg" onclick="handleLeftClick(this);" oncontextmenu="handleRightClick(this); return false;" />
</body>
</html>

For more information, check out http://www.w3schools.com/html5/html5_ref_eventattributes.asp

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜