我想打印div的内容并在打印页面上添加一些图像。我在Stackoverflow(Print the contents of a DIV)
上找到了解决方案但是,虽然我在页面上添加了图像,但图像不会出现在打印页面上。
function PrintElem(elem){var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write('<p><center>' + document.getElementById(elem).innerHTML + '</center></p>');
mywindow.document.write('<img src="https://www.google.com.tr/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">');
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
示例Google图片未显示在printpreview页面上。
答案 0 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<script>
function PrintElem(elem)
{
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body>');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write('<p><center>' + document.getElementById(elem).innerHTML + '</center></p>');
mywindow.document.write('<img src="https://www.google.com.tr/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"/>');
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
}
</script>
</head>
<body>
<h2>My First JavaScript</h2>
<button type="button" onclick="PrintElem('aa');">
Click me to display Date and Time.</button>
<div id="aa"> my sample</div>
<p id="demo"></p>
</body>
</html>
参考上面的代码。它工作正常