问题描述
我可以进入contextmenu对象并禁用它(),但是当用户右键单击链接对象并选择在新标签页中打开时,如何替换链接对象中的原始href还是在新窗口中打开或在隐身窗口中打开?
I can get into contextmenu object and disable it (How to add a custom right-click menu to a webpage?), but how can I replace original href from a link object, when user right-click on it and choose "open in a new tab" or "open in a new window" or "open in an incognito window"?
推荐答案
事实上,我发现了一个更好/更简单的方法实现它的方法。 replaceLink()负责在此处替换centextmenu链接:
In fact I found a better/simpler way to achieve it. replaceLink() is responsible for replacing centextmenu links here:
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<a href="https://majkesz.pl" id="lol" oncontextmenu="replaceLink(event);">majkesz.pl</a><br>
<script>
document.getElementById("lol").onclick = function(event) {
event.preventDefault();
window.location.href = "https://www.youtube.com/watch?v=oHg5SJYRHA0";
return false;
};
function replaceLink(e) {
e.target.href = "https://www.youtube.com/watch?v=oHg5SJYRHA0";
}
</script>
</body>
</html>
不幸的是,上述解决方案不适用于FF和更新的Chrome浏览器的鼠标中键。而是使用通用:
unfortunatelly above solution is not working for middle click of mouse for FF and newer chrome. instead use generic:
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<a href="https://majkesz.pl" onmousedown="replaceLink(event)" oncontextmenu="replaceLink(event);">majkesz.pl</a><br>
<script>
function replaceLink(e) {
e.target.href = "https://www.youtube.com/watch?v=oHg5SJYRHA0";
}
</script>
</body>
</html>
这篇关于右键单击并选择“打开...”之一后,从上下文菜单替换href链接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!