如何禁用“在新选项卡中打开链接”在浏览器中?
如何禁用/删除浏览器右键菜单中的“在新选项卡中打开链接”选项? 如果这对于 javascript 等来说是不可能的,那么有没有办法修改用户单击此链接时发生的情况,例如,显示警报或阻止选项卡加载..?
How do I disable/remove the 'Open Link in New Tab' option in the right-click menu of the browser?
If this is not possible with javascript etc then is there a way to modify what happens when the user clicks on this link, for example, display an alert or prevent the tab from loading..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在新选项卡/窗口中打开链接的能力是许多浏览器的本机功能。如果您不希望允许这种类型的活动,那么您需要通知浏览器您的链接不是真正的链接。最简单的方法是从
a
元素中删除href
属性。HTML:
现在,浏览器在看到链接时可能会默认为您执行一些其他操作。如果您尚未定义
a
元素的任何样式,则新的精美伪链接可能不会显示链接字体颜色、指针和下划线。您可以继续轻松地做到这一点。CSS:
这有望回答如何禁用/删除浏览器右键菜单中的“在新选项卡中打开链接”选项的问题。不过,为了获得一些额外的好处,我假设您可能希望该链接在单击时仍然像常规链接一样发挥作用。请随意使用一些 JavaScript 来实现这一点。下面是一个使用 jQuery 的示例:
JavaScript:
修改的 Html:
修改的 CSS:
希望这会有所帮助!
The ability to open a link in a new tab/window is native functionality of many browsers. If you do not wish to allow this type of activity, then you need to notify the browser that your link is not truly a link. The easiest way to do so is to remove the
href
attribute from youra
element.HTML:
Now there are some other things that the browser may be doing for you by default when it sees a link. If you have not defined any styling of
a
elements, it's likely that your new fancy pseudo-link will not appear with a link font-color, pointer, and underline. You can go ahead and do that easily enough.CSS:
That hopefully answers the question of how you disable/remove the 'Open Link in New Tab' option in the right-click menu of the browser. For some extra credit though I'm going to assume that you probably want the link to still function like a regular link when clicked. Feel free to use some JavaScript to make that happen. Here's an example using jQuery:
JavaScript:
Modified Html:
Modified CSS:
Hope this helps!
2019 年,
我将重点强调 2 条重要评论:
nnnnnn @ 2011 年 8 月 10 日 5:49 :
<块引用>
作为用户,我更喜欢从以下位置触发该类型的功能:
按钮而不是链接。我希望链接能够以更标准的方式工作
方式包括给我右键单击菜单或
单击中键直接在新选项卡中打开。
帕尔斯 @ 2013 年 12 月 21 日 13:10 :
<块引用>
当您单击鼠标中键或按住 CTRL 并按住时,此操作将不起作用
单击链接。如何处理这些情况。
由浏览器决定是否获取链接行为。
如果全部 以下条件匹配:
...
标记(不是span
、div
、<代码>按钮等)。href
属性。href
属性不为空。下面将获得链接行为:
下面不会获得链接行为:
重要提示:
同样,这取决于浏览器决定。如果上述 3 个条件中的任何一个匹配,一些旧/愚蠢的浏览器可能会给出链接行为!
结论
如果它是链接,则使用
...
。如果它不是链接,则使用其他内容,例如2019
I'll highlight 2 important comments:
nnnnnn @ Aug 10 '11 at 5:49 :
Pars @ Dec 21 '13 at 13:10 :
It's up to the browser to decide whether it gets the link-behavior or not.
Some modern browser (like Chrome) will consider it a link if all the below conditions matches:
<a>...</a>
tag (notspan
,div
,button
, etc).href
attribute.href
attribute is not empty.The below will get the link-behavior:
The below will NOT get the link-behavior:
Important Note:
Again, it's up to the browser to decide. Some old/stupid browsers may give it link-behavior if ANY of the 3 conditions above matches!
Conclusion
If it's a link then use
<a href="some_link">...</a>
. If it's not a link then use something else, like<button>
.我不知道为什么有人会否决你的问题,你需要谷歌的是如何使用 javascript 劫持事件。当您单击链接并阻止其工作时,您可以很好地显示警报。 jQuery 是一个 populat JS 工具包,有一个方法可以停止元素上的默认事件,用简单的英语来说,使用 jQuery,您可以停止超链接的工作并显示警报。此链接 http://api.jquery.com/event.preventDefault/ 可以进一步帮助您。关于禁用右键单击网页时出现的上下文菜单,是的,这是很有可能的。我不是 JS 忍者,所以无法评论是否可以从上下文菜单中删除某个项目,但在我看来,这是不可能的。
I am not sure why would some one downvote your question, what you need to google is how to hijack events using javascript. You can very well show an alert when you click a link and stop it from working. jQuery is a populat JS toolkit and has a method to stop default events on an element, in plain English, using jQuery you can stop a hyperlink from working it and show an alert as well. This link http://api.jquery.com/event.preventDefault/ can help you further. Regarding disabling the context menu that appears on right clicking on a webpage, yes that is very much possible. I am not a JS ninja so can't comment if you can remove an item from context menu but IMO, it should not be possible.
现在已经解决了主要问题,非常容易,因为只需将 href 的查询字符串传递给 ajax 函数即可。
我可能应该在我的问题中解释说我想要的只是作为临时措施。无论如何,感谢所有的评论。
Have solved the main issue now, quite easily as it turned out just by passing the query string of the href to an ajax function.
I probably should have explained in my question that what I wanted was intended only as a temporary measure. Anyway thanks for all the comments.