Print to print a webpage is a simple task, but sometimes we want to print the content of a specified area. Next I will introduce how to print the webpage code in a specified area using print.
Relatively simple method
Use CSS to define a. noprint class and put non-printed content into this class.
Details:
The Code is as follows: |
Copy code |
<Style media = print type = "text/css"> . Noprint {visibility: hidden} </Style> |
The content to print. Haha!
The Code is as follows: |
Copy code |
<P class = "noprint"> put the unprinted code here. </P> <A href = "distinct Cr partition pt: window. print ()" target = "_ self"> print </a> |
Method 2 Use js to operate
The Code is as follows: |
Copy code |
<Script language = "javascript"> Function preview () { Bdhtml###doc ument. body. innerHTML; Sprnstr = "<! -- Startprint --> "; Eprnstr = "<! -- Endprint --> "; Prnhtml = bdhtml. substr (bdhtml. indexOf (sprnstr) + 17 ); Prnhtml = prnhtml. substring (0, prnhtml. indexOf (eprnstr )); Too many Doc ument. body. innerHTML = prnhtml; Window. print (); } </Script> <Div> the file header. No output is displayed... </Div> <Div> the file header. No output is displayed... </Div> <Div> the file header. No output is displayed... </Div> <! -- Startprint --> <Div> This is the printed content </div> <Div> This is the printed content </div> <Div> This is the printed content </div> <Div> This is the printed content </div> <! -- Endprint --> <Div> The End of the file. No output is displayed... </Div> <Div> The End of the file. No output is displayed... </Div> <Div> The End of the file. No output is displayed... </Div>
<Input type = "button" name = "print" value = "preview and print" onclick = "preview ()"> |
Alternatively
The Code is as follows: |
Copy code |
<Script language = "javascript" type = "text/javascript"> Function printpage (myDiv ){ // Var newstr = document. all. item (myDiv). innerHTML; Var newstr = document. getElementById (myDiv). innerHTML; Var oldstr = document. body. innerHTML; Document. body. innerHTML = newstr; Window. print (); Document. body. innerHTML = oldstr; Return false; } </Script> <Div id = "myDiv"> Content </div> <Input type = "button" id = "bt" onclick = "javascript: printpage ('mydiv ')" value = "print"/> |
We have not tested the compatibility of js above. We can use jquery to set it.
The Code is as follows: |
Copy code |
<Html> <Head> <Title> jquery prints the content of a specified region </title> <Script src = "http://code.jquery.com/jquery-1.7.2.min.js"> </script> <Script type = "text/javascript"> Function printHtml (html ){ Var bodyHtml = document. body. innerHTML; Document. body. innerHTML = html; Window. print (); Document. body. innerHTML = bodyHtml; } Function onprint (){ Var html = $ ("# printArea" ).html (); PrintHtml (html ); } </Script> </Head> <Body> <Div> <Div id = "printArea" style = "width: 500px; text-align: left;"> Print area ~~~~ </Div> <Br/> <Div> <Input type = "button" id = "btnPrint" onclick = "onprint ()" value = "print"/> </Div> </Div> </Body> </Html> |
Let's take a look at a method of my own instance. The principle is as follows:
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 () call the printer to print the current document
JavaScript print function myPrint (obj ):
The Code is as follows: |
Copy code |
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: MyPrint (document. getElementById ('printdidid ')); |
Example
The Code is as follows: |
Copy code |
<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> |