Front-end js browser printing

window.print() is the method to call the browser to print. But the default is to print the entire page of the page where the window is currently located. Is there a way to print only a certain area, or in other words, only print the elements in a certain Dom element?

function BrowserPrint (Dom){
    
    
	window.document.body.innerHTML = Dom.innerHTML;
    window.print(); 
    // 打印完成以后一定要重新刷新页面,因为window被要打印的Dom取代了
    window.location.reload();
}

const printDom = window.document.getElementById('content-area');
BrowserPrint (printDom);

Encountering a demand, printing the file pointed to by the iframe tag src is a bit troublesome, and it is still being explored. It may be due to cross-domain issues that make it difficult to obtain the contentWindow of the iframe, and there is no way to call the print method of the iframe subpage. Up.

Guess you like

Origin blog.csdn.net/glorydx/article/details/114768894