Html 如何同时使用 onclick 和 target="_blank"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19588708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use both onclick and target="_blank"
提问by Patidati
Code is as following:
代码如下:
<p class="downloadBoks" onclick="location.href='Prosjektplan.pdf'">Prosjektbeskrivelse</p>
Works fine like this, but it opens the file in the same window. I want to apply the target="_blank". But after some googleing I still can't figure it out.
像这样工作正常,但它在同一窗口中打开文件。我想应用目标=“_blank”。但经过一些谷歌搜索后,我仍然无法弄清楚。
回答by Praveen Kumar Purushothaman
Instead use window.open()
:
而是使用window.open()
:
The syntax is:
语法是:
window.open(strUrl, strWindowName[, strWindowFeatures]);
Your code should have:
你的代码应该有:
window.open('Prosjektplan.pdf');
Your code should be:
你的代码应该是:
<p class="downloadBoks"
onclick="window.open('Prosjektplan.pdf')">Prosjektbeskrivelse</p>
回答by Haimei
onclick="window.open('your_html', '_blank')"
回答by DontVoteMeDown
Just use window.open()
:
只需使用window.open()
:
window.open('Prosjektplan.pdf')
Anyway, what guys are saying on comments is true. You better use <a target="_blank">
instead of click events.
无论如何,人们在评论中所说的都是真实的。你最好使用<a target="_blank">
而不是点击事件。
回答by Phil Allen
The window.open method is prone to cause popup blockers to complain
window.open 方法容易导致弹窗拦截器报错
A better approach is:
更好的方法是:
Put a form in the webpage with an id
在网页中放置一个带有 id 的表单
<form action="theUrlToGoTo" method="post" target="yourTarget" id="yourFormName">
</form>
<form action="theUrlToGoTo" method="post" target="yourTarget" id="yourFormName">
</form>
Then use:
然后使用:
function openYourRequiredPage() {
var theForm = document.getElementById("yourFormName");
theForm.submit();
}
}
and
和
onclick="Javascript: openYourRequiredPage()"
onclick="Javascript: openYourRequiredPage()"
You can use
您可以使用
method="post"
method="post"
or
或者
method="get"
method="get"
As you wish
如你所愿
回答by junior_software
you can use
您可以使用
<p><a href="/link/to/url" target="_blank"><button id="btn_id">Present Name </button></a></p>