Implementation Code of javascript partial page Printing

Source: Internet
Author: User

Ie comes with Active controls, but Firefox does not. The Javascript dom method is used to print the text in the specified mark of the form. Therefore, you need to define the related labels and styles (such as the text size and font) for use ).

Copy codeThe Code is as follows:
<Script type = "text/javascript">
///***********************
/// Print the page of the specified region
/// Description: obj-get the tag ID through getElementById or other methods, and print the text in this obj
/// Date: 2009-8-7
Function startPrint (obj)
{
Var oWin = window. open ("", "_ blank ");
Var strPrint = "


StrPrint = strPrint + "<script type = \" text/javascript \ "> \ n ";
StrPrint = strPrint + "function printWin () \ n ";
StrPrint = strPrint + "{";
StrPrint = strPrint + "var oWin = window. open (\" \ ", \" _ blank \ "); \ n ";
StrPrint = strPrint + "oWin.doc ument. write (document. getElementById (\" content \ "). innerHTML); \ n ";
StrPrint = strPrint + "oWin. focus (); \ n ";
StrPrint = strPrint + "oWin.doc ument. close (); \ n ";
StrPrint = strPrint + "oWin. print () \ n ";
StrPrint = strPrint + "oWin. close () \ n ";
StrPrint = strPrint + "} \ n ";
StrPrint = strPrint + "<\/script> \ n ";

StrPrint = strPrint + "StrPrint = strPrint + "<div id = \" content \ "> \ n ";
StrPrint = strPrint + obj. innerHTML + "\ n ";
StrPrint = strPrint + "</div> \ n ";
StrPrint = strPrint + "StrPrint = strPrint + "<div style = 'text-align: Center'> <button onclick = 'printwin () 'style = 'padding-left: 4px; padding-right: 4px; '> Print </button> <button onclick = 'window. opener = null; window. close (); 'style = 'padding-left: 4px; padding-right: 4px; '> close </button> </div> \ n ";
OWin.doc ument. write (strPrint );
OWin. focus ();
OWin.doc ument. close ();
}
</Script>

<Button id = "btnPrint" onclick = "startPrint (document. getElementById ('content')"> print content </button>
<Div id = "content">
<Div style = "font-size: 12px; color: #333;">
Here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content
</Div>
</Div>

The following is the test code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> Print the page of the specified area </title> <style type =" text/css "> body {font-size: 12px; color: #333; }# content {border: solid 1px #333; background: # ffc ;} </style> </pead> <body> <button id = "btnPrint" onclick = "startPrint (document. getElementById ('content ')) "> print content </button>: print content here: print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content print content here print content here </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

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.