How to specify the div area on a JavaScript page: Use window. open () open a new page (window) in the browser, and use expose Doc ument. write () writes the content of the specified div area to the new window document, document. close () close the document and use window. print () calls the printer to print the current document.
JavaScript print function myPrint (obj ):
Copy codeThe Code is as follows:
Function myPrint (obj ){
// Open a new window newWindow
Var newWindow = window. open ("Print window", "_ blank ");
// The content of the div to be printed
Var docStr = obj. innerHTML;
// Print the content and write it to the newWindow document.
Newclientdoc ument. write (docStr );
// Close the document
Newmediaworkflow Doc ument. close ();
// Call the printer
NewWindow. print ();
// Close the newWindow page
NewWindow. close ();
}
Myprint () call method:
Copy codeThe Code is as follows: myPrint (document. getElementById ('printdidid '));
Instance code:
Copy codeThe Code is as follows: <script>
Function myPrint (obj ){
Var newWindow = window. open ("Print window", "_ blank ");
Var docStr = obj. innerHTML;
Newclientdoc ument. write (docStr );
Newmediaworkflow Doc ument. close ();
NewWindow. print ();
NewWindow. close ();
}
</Script>
<Div id = "print">
<Hr/>
Print the demo area. Click print to load the content in the new window!
<Hr/>
</Div>
<Button onclick = "myPrint (document. getElementById ('print ')"> print </button>