下面是我的代码,可在所有操作系统的所有浏览器中使用,但ipad chrome除外。在这里帮助我。
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<script>
function myprint() {
const wnd = window.open('about:blank', '', '_blank, alwaysRaised=yes');
wnd.document.write('<html><head><title></title>');
wnd.document.write('</head><body>');
wnd.document.write('<div>JavaScript often abbreviated as JS, is a high-level, interpreted programming language. It is a language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.</div>');
wnd.document.write('<div class="print-header"><button title="Print" onclick="window.print()">Print</button></div>');
wnd.document.write('</body></html>');
wnd.document.close();
}
</script>
<body>
<button onclick="myprint()">popup</button>
</body>
</html>
我在这里尝试使用window.open()
打开我的内容,然后使用window.print()
打印它们。就这样。 jsfiddle
此链接在ipad chrome中也不起作用。 Print
答案 0 :(得分:0)
尝试使用此代码,如果它不起作用,另一解决方案是使用像这样的第三方打印服务:http://www.printfriendly.com
function goPrint(){
window.print();
if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
window.location.reload();
}
}
答案 1 :(得分:0)
您在此行中使用反引号`
:
wnd.document.write(`<div class="print-header"><button title="Print" onclick="window.print()">Print</button></div>`);
这是Templated literal,可能不受支持。尝试用单引号将其替换:
wnd.document.write('<div class="print-header"><button title="Print" onclick="window.print()">Print</button></div>');