代码如下:
<p class="downloadBoks" onclick="location.href='Prosjektplan.pdf'">Prosjektbeskrivelse</p>
像这样工作正常,但它在同一窗口中打开文件。我想应用 target="_blank"。但经过一番谷歌搜索后我仍然无法弄清楚。
window.open()
:
语法是:
window.open(strUrl, strWindowName[, strWindowFeatures]);
您的代码应该具有:
window.open('Prosjektplan.pdf');
您的代码应该是:
<p class="downloadBoks"
onclick="window.open('Prosjektplan.pdf')">Prosjektbeskrivelse</p>
onclick="window.open('your_html', '_blank')"
你可以使用
<p><a href="/link/to/url" target="_blank"><button id="btn_id">Present Name </button></a></p>
window.open方法容易导致弹出窗口拦截器抱怨
更好的方法是:
在网页中放置一个带有 id 的表单
<form action="theUrlToGoTo" method="post" target="yourTarget" id="yourFormName">
</form>
然后使用:
function openYourRequiredPage() {
var theForm = document.getElementById("yourFormName");
theForm.submit();
}
和
onclick="Javascript: openYourRequiredPage()"
你可以使用
method="post"
或
method="get"
如你所愿
所有这些解决方案都无法正常工作来解决您必须将字符串放入变量中并将其全部包装在 iframe 中的问题,如下所示:
const newWindow = window.open();
newWindow.document.write(`
<html>
<body style="margin: 0; overflow: hidden;">
<iframe
src="${pdfUrl}"
title="PDF Viewer"
style="width: 100%; height: 100vh; border: none;"
></iframe>
</body>
</html>
`);
};