When dealing with web pages, there are instances where you may need to extract and print the content within a specific DIV. Let's delve into the best way to achieve this.
To effectively print the contents of a DIV, consider using the following function:
function PrintElem(elem) { var mywindow = window.open('', 'PRINT', 'height=400,width=600'); mywindow.document.write('<html><head><title>' + document.title + '</title>'); mywindow.document.write('</head><body >'); mywindow.document.write('<h1>' + document.title + '</h1>'); mywindow.document.write(document.getElementById(elem).innerHTML); mywindow.document.write('</body></html>'); mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10*/ mywindow.print(); mywindow.close(); return true; }
To use this function, you need to call it with the ID of the DIV you want to print as the argument. For instance:
PrintElem('myDiv');
This function opens a new browser window with the title of your document, followed by the title of the DIV. The content of the DIV is then printed in the new window.
The above is the detailed content of How Can I Print the Content of a Specific DIV Element on a Web Page?. For more information, please follow other related articles on the PHP Chinese website!