如何禁用右键单击我的网页? [英] How do I disable right click on my web page?
问题描述
我可以在不使用JavaScript的情况下禁用右键单击我的网页吗?我问这是因为大多数浏览器允许用户禁用JavaScript。
Can I disable right click on my web page without using JavaScript? I ask this because most browsers allow user to disable JavaScript.
如果没有,我如何使用JavaScript禁用右键?
If not, how do I use JavaScript to disable right click?
推荐答案
您可以通过为contextmenu事件添加事件监听器并调用 preventDefault()
方法:
You can do that with JavaScript by adding an event listener for the "contextmenu" event and calling the preventDefault()
method:
document.addEventListener('contextmenu', event => event.preventDefault());
这就是说:不要做。
为什么?因为它除了烦人的用户之外什么都没有。此外,许多浏览器都有一个安全选项,禁止禁用右键单击(上下文)菜单。
Why? Because it achieves nothing other than annoying users. Also many browsers have a security option to disallow disabling of the right click (context) menu anyway.
不确定为什么要这样做。如果出于某种错误的信念,你可以用这种方式保护你的源代码或图像,那就再想一想:你做不到。
Not sure why you'd want to. If it's out of some misplaced belief that you can protect your source code or images that way, think again: you can't.
这篇关于如何禁用右键单击我的网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!