使用JavaScript和Google Chrome创建并打开新窗口并打印图像
当我尝试使用使用JavaScript和Google Chrome创建并打开新窗口并打印图像,javascript,html,css,google-chrome,Javascript,Html,Css,Google Chrome,当我尝试使用JavaScript的window.print()打开并打印窗口时,图像在打印时不会显示在Chrome中。我的代码如下: <html> <head> <script type="text/javascript"> function openWin() { var myWindow = window.open('','', 'width=200,height=100'); myWindow.document.write("&
JavaScript
的window.print()打开并打印窗口时,图像在打印时不会显示在Chrome
中。我的代码如下:
<html>
<head>
<script type="text/javascript">
function openWin()
{
var myWindow = window.open('','', 'width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.write("<p><img src='https://www.google.gr/images/srpr/logo11w.png' width='400' /></p>");
myWindow.document.close();
myWindow.focus();
myWindow.print();
myWindow.close();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
函数openWin()
{
var myWindow=window.open('','',宽度=200,高度=100');
myWindow.document.write(“这是‘myWindow’”);
myWindow.document.write(“”);
myWindow.document.close();
myWindow.focus();
myWindow.print();
myWindow.close();
}
你可以在这里查一下
如何解决这个问题 调用
print()
时,图像尚未加载
您需要延迟调用print
,直到图像加载完成。jQuery可以做到这一点,因此您可以在页面中包含jQuery,然后将对print
的调用封装在如下代码中:
function openWin()
{
var myWindow = window.open('','','width=500,height=500');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.write("<p><img src='https://www.google.gr/images/srpr/logo11w.png' width='400' /> </p>");
myWindow.document.write("<script src='http://code.jquery.com/jquery-1.10.1.min.js'></script>");
myWindow.document.write("<script>$(window).load(function(){ print(); });</script>");
}
函数openWin()
{
var myWindow=window.open('','',宽度=500,高度=500');
myWindow.document.write(“这是‘myWindow’”);
myWindow.document.write(“”);
myWindow.document.write(“”);
myWindow.document.write($(window.load)(function(){print();}););
}
我将在“窗口”的何处添加窗口名?“我的窗口”?或者没有必要,我已经编辑了我的答案。试试看。JSFIDLE不支持document.write,所以不必费心在那里尝试:-)这里可以工作,但只有在第一次。需要重新加载才能再次工作