JS Print Print Web page Specify area content

Source: Internet
Author: User
Tags print print

Easier way.

Using CSS, define a. Noprint class, which will not be printed into this class.

Detail as follows:

The code is as follows Copy Code

<style media=print type= "Text/css" >

. Noprint{visibility:hidden}

</style>

The content to be printed. Ha ha!

The code is as follows Copy Code

<p class= "noprint" > will not print the code here. </p>

<a href= "Javascrīpt:window.print ()" target= "_self" > Printing </a>

Method two uses JS to operate

The code is as follows Copy Code

<script language= "JavaScript" >
Function Preview ()
{
bdhtml=window.document.body.innerhtml;
Sprnstr= "<!--startprint-->";
Eprnstr= "<!--endprint-->";
Prnhtml=bdhtml.substr (Bdhtml.indexof (SPRNSTR) +17);
Prnhtml=prnhtml.substring (0,prnhtml.indexof (EPRNSTR));
window.document.body.innerhtml=prnhtml;
Window.print ();
}
</script>


<div> file head, not print out the content ... </div>
<div> file head, not print out the content ... </div>
<div> file head, not print out the content ... </div>
<!--startprint-->
<div> This is the content that is printed </div>
<div> This is the content that is printed </div>
<div> This is the content that is printed </div>
<div> This is the content that is printed </div>
<!--endprint-->
<div> the tail of the file, do not print out the content ... </div>
<div> the tail of the file, do not print out the content ... </div>
<div> the tail of the file, do not print out the content ... </div>

<input type= "button" name= "print" value= "Preview and print" onclick= "Preview ()" >

Or maybe that's okay.

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"/>

The above JS has not tested compatibility we can use jquery to set

  code is as follows copy code

<title>jquery print specified area content </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>
<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>

Here's a way to look at my own example, the principle is this

JavaScript print page Specifies the div area principle: Use window.open () to open a new page (window) in the browser, and use Window.document.write () to write the contents of the specified Div area to the new window document. Document.close () Closes the document and uses Window.print () to 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 contents of the DIV to print
var docstr = obj.innerhtml;
Print content write to NewWindow document
NewWindow.document.write (DOCSTR);
Close Document
NewWindow.document.close ();
Calling a printer
Newwindow.print ();
Close NewWindow Page
Newwindow.close ();
}
Myprint () Call method:

Myprint (document.getElementById (' Printdivid '));

Cases

  code is as follows copy code

<script
function Myprint (obj) {
    var newwindow=window.open ("Print Window", "_blank");
     var docstr = obj.innerhtml;
    NewWindow.document.write (DOCSTR);
    newWindow.document.close ();
    newwindow.print ();
    newwindow.close ();
}
</script>
<div id= "print"
   Print the demo area, click Print to load the contents of the new window!
</div>
<button onclick= "myprint (' print ')" > Print document.getElementById Button>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.