如何禁用或隐藏超链接的“在新标签页中打开”选项

问题描述:

在右键菜单中的超链接上,如何删除或隐藏在新标签页中打开在新窗口中打开选项?

On hyperlinks in the right click menu, how can I remove or hide the Open In New Tab and Open In New Window options?

例如

<a href="#" onclick="asd">foo</a>


不确定为什么要这样做但是可以通过将 href 移动到 data-href 属性来完成,然后删除 href 并添加一个点击处理程序。 onclick将读取 data-href 并重定向。

Not sure why you'd want to do this but it can be done by moving the href to a data-href attribute, then remove the href and add a click handler. The onclick will read the data-href and redirect.

演示

var links = document.getElementsByTagName("a");

for(var i=0; i<links.length; i++){
    links[i].setAttribute("data-href", links[i].getAttribute("href"));
    links[i].removeAttribute("href");
    links[i].onclick = function(){
        window.location = this.getAttribute("data-href");
    };
}

右键菜单显示: