Chrome、Opera 和 IE 中的右键单击事件(onclick)
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
右键单击会调用大多数标准浏览器中的上下文菜单;因此,您可以使用“oncontextmenu”侦听器来处理右键单击事件。如果您不希望监听器在调用 JS 代码后显示标准浏览器上下文菜单,则监听器应返回 false。
下面是一些处理图像左键和右键单击的示例 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.
For more information, check out http://www.w3schools.com/html5/html5_ref_eventattributes.asp