gpt4 book ai didi

javascript - 使用 JavaScript 和 Google Chrome 创建和打开新窗口并打印图像

转载 作者:可可西里 更新时间:2023-11-01 12:48:50 26 4
gpt4 key购买 nike

当我尝试使用 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>

你可以在这里查看http://jsfiddle.net/Q5Xc9/649/如何解决这个问题?

最佳答案

调用 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>");
}

关于javascript - 使用 JavaScript 和 Google Chrome 创建和打开新窗口并打印图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20903573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com