我可以进入 contextmenu 对象并禁用它( How to add a custom right-click menu to a webpage? ),但是当用户右键单击它并选择“在新选项卡中打开”或“在其中打开”时,如何替换链接对象中的原始 href新窗口”或“在隐身窗口中打开”?
最佳答案
事实上,我找到了一种更好/更简单的方法来实现它。 ReplaceLink() 负责替换这里的 centextmenu 链接:
<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 的鼠标中键单击。而是使用通用的:
<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>
关于javascript - 右键单击并选择 "open..."之一后替换上下文菜单中的 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54420012/