javascript - 当用户在新窗口中打开时,如何使 onclick"window.location" 也能工作

Ste*_*ven 1 javascript javascript-events

要隐藏链接的真实目的地,我使用以下代码:

<a href="http://example.com" onclick="window.location='http://url-i-want-to-hit.com/'; return false;">Click me</a>
Run Code Online (Sandbox Code Playgroud)

但是,如果我右键单击然后在新窗口中打开,它会转到锚点 href 标记中指定的网址。我希望它转到 javascript 中指定的 url。

有没有办法做到这一点?

提前致谢

Jos*_*ola 5

你所需要的只是一个 mousedown 处理程序来伪造它们......

<a href="http://example.com"
   onmousedown="this.href2 = this.href;
                this.href = 'http://url-i-want-to-hide.com/';"
   onmouseout="if(this.href2) this.href = this.href2;">Test</a>
Run Code Online (Sandbox Code Playgroud)

这是它工作的现场演示