我有一个 .Net 应用程序,它动态创建一个小型 HTML 页面,并使用 javascript document.open 方法将其弹出在新窗口中。具有该功能的一切都工作正常。
现在我想向打印该页面的 HTML 页面添加一个按钮。我尝试使用以下代码但无济于事:
<a href='print.html' onClick='window.print();return false;'>
<img src='images/printer.png' height='32px' width='32px'></a>
当在弹出窗口中单击该按钮时,没有任何反应。但是,当该页面的源代码作为单独的页面保存并加载到浏览器中时,打印按钮可以正常工作。 因此看来问题是由于代码位于弹出窗口中而引起的。 [现在的问题似乎是代码在弹出窗口打开后写入。]有谁知道解决这个问题的方法或任何替代方案?
EDIT:
我尝试过的其他方法具有相同的结果:
<input type='button' onclick='window.print()' value='Print' />
and
<a href='javascript:window.print()'>
<img src='images/printer.png' height='32px' width='32px'></a>
再次编辑:
上面的代码在 Firefox 中有效,但在 IE7 中无效。关于 IE 的解决办法有什么想法吗?
再次编辑:
这是一个使用代码的测试用例npup https://stackoverflow.com/users/294150/npup posted https://stackoverflow.com/questions/2448676/how-do-you-print-from-a-popup-window-in-javascript/2460794#2460794以下。我没有将弹出窗口的代码放在单独的 html 文件中,而是打开一个空白 url,然后将代码写入其中。此步骤似乎是导致问题的原因。
<html>
<head>
<title>main</title>
</head>
<body>
<h1>
Pop & print</h1>
<button onclick="pop();">
Pop</button>
<script type="text/javascript">
var POP;
function pop() {
var newWin = window.open('', 'thePopup', 'width=350,height=350');
newWin.document.write("<html><head><title>popup</title></head><body><h1>Pop</h1>" +
"<p>Print me</p><a href='print.html' onclick='window.print();return false;'>" +
"<img src='images/printer.png' height='32px' width='32px'></a></body></html>");
}
</script>
</body>
</html>