1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>打印指定区域</title> </head> <body>
<div> <button onclick="printDiv()">打印指定内容</button> </div>
<div id="printArea"> <h2>这是需要打印的内容</h2> <p>这里的内容将会被打印。</p > </div>
<div> <p>这里是不会被打印的内容。</p > </div>
<script> function printDiv() { var divContent = document.getElementById("printArea").innerHTML; var newWindow = window.open('', '', 'width=800,height=600'); newWindow.document.write('<html><head><title>打印</title></head><body>'); newWindow.document.write(divContent); newWindow.document.write('</body></html>'); newWindow.document.close(); newWindow.print(); } </script>
</body> </html>
|