我有一个代码突出显示 block ,我希望有一个选项,您可以单击按钮,它会打开一个新的 HTML 页面,在其中显示突出显示的“原始”内容。我已准备好原始形式的代码和使用 target="_blank"准备的链接,但我似乎无法让它打开一个新页面。
这就是我的 HTML 的样子:
<a href="#" target="_blank">Click to view HTML</a>
这是我的 JavaScript
//when clicked
link.href = 'javascript:document.write("...");';
//the click event should continue as normal
这应该打开一个以“...”为内容的新页面,但它不起作用(它只是打开现有页面)。
是否有办法在不使用弹出窗口的情况下执行此操作?
最佳答案
function writeToWindow(content) {
var newWin = window.open('','newWin','width=300,height=200');
newWin.document.open();
newWin.document.writeln("<html><head><title>Console</title></head><body>" + content + "</body></html>");
newWin.document.close();
}
调用它
onclick="writeToWindow('text to display');"
关于javascript - 目标 ="_blank"带有自定义 javascript 生成的 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12076812/