2.方法二
将需要打印的内容用一个大的div包裹,打印时将body的内容替换为该div的内容,调用打印print()方法。
<
body
>
<
input
type
=
"button"
value
=
"打印此页面"
onclick
=
"printpage()"
/>
<
div
id
=
"printContent"
>打印内容</
div
>
<
script
>
function printpage() {
let newstr = document.getElementById("printContent").innerHTML;
let oldstr = document.body.innerHTML;
document.body.innerHTML = newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</
script
>
</
body
>
标签:body,打印,innerHTML,JS,window,print,div,document
From: https://www.cnblogs.com/shaowangwu/p/16825455.html