Chrome、Opera 和 IE 中的右键单击事件(onclick)

发布于 2024-09-15 20:46:00 字数 194 浏览 1 评论 0原文

在 Firefox 中,我使用了 document.onclick 事件,然后检查它是否是右键单击,如果我右键单击,则一切都会按预期进行,但在 Chrome、Opera 和 IE8 中,如果我右键单击 < code>document.onclick 不会触发。

我想要一个用于 img 元素的自定义上下文菜单。我该怎么办?

In Firefox I used the document.onclick event and then checked 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-09-22 20:46:00

右键单击会调用大多数标准浏览器中的上下文菜单;因此,您可以使用“oncontextmenu”侦听器来处理右键单击事件。如果您不希望监听器在调用 JS 代码后显示标准浏览器上下文菜单,则监听器应返回 false。

下面是一些处理图像左键和右键单击的示例 html。

<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>

有关详细信息,请查看 http://www.w3schools.com/html5/html5_ref_eventattributes.asp

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文