如何在HTML中打开一个新窗口来访问URL?

3
这是代码:

这是代码:

<a href="FAKE_URL" onclick="document.location.href = 'REAL_URL'; return false;">
<img src="IMAGE"></a>

我已经尝试过:

<a href="FAKE_URL" onclick="document.location.href = 'REAL_URL'; return false;" target="_blank">
<img src="IMAGE"></a>

但是没有起作用


你为什么想要在那里使用onclick事件? - Mr. Alien
<input type="button" name="XX" onclick="window.open('yourfile.html','','width=750px,height=1000px,left=150,top=0,toolbar=no,status=no,resizable=no,titlebar=no').focus();"> - Hearner
3个回答

1

试试这个:

<a href="../html-link.html" target="popup" onclick="window.open('../html-link.html','name','width=600,height=400')">Open page in new window</a>

1

您可以使用window.open('url','_blank')在单击事件后打开一个新标签页:

<a href="https://google.com" onclick="window.open('https://youtube.com','_blank'); return false;">click here for a new tab</a>

或者您可以使用 window.open('url','','width=,height=') 打开一个新窗口:
<a href="https://google.com" onclick="window.open('https://youtube.com','','width=800,height=700'); return false;">click here for a new window...</a>

在这个例子中,它将打开youtube (REAL_URL) 而不是google (FAKE_URL)。

> JSFiddle - 示例


说明:

你的错误在于添加 target="_blank" 改变了你的 <a href="..."></a> 的行为,但没有改变你的 document.location.href='...'。因此,如果没有 document.location.href='...',它会在一个新标签页中打开FAKE_URL


0

要在新窗口中打开文件,请使用

onclick="window.open('yourfile.html','','width=750px,height=1000px,left=150,top=‌​0,toolbar=no,status=no,resizable=no,titlebar=no').focus();"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接