Home > Article > Web Front-end > ✨ Simple trick for printing a div
@media print { body { visibility: hidden; } }
@media print { #section-to-print { top: 0; left: 0; position: absolute; visibility: visible; } }
Then print
const button = document.querySelector('print-page'); button.addEventListener('click', () => window.print());
This method avoids the pitfall of loosing interactivity, other methods replace the entire page content with the static HTML thereby loosing interactivity.
const printContents = document.getElementById(divId).innerHTML; const originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents;
The above is the detailed content of ✨ Simple trick for printing a div. For more information, please follow other related articles on the PHP Chinese website!