Using JavaScript to implement print _javascript techniques for a specified area in a Web page

Source: Internet
Author: User

Recently did the Web page schedule printing, the Internet to find some information, and finally use the following method to achieve the functions I need. Place the table in the DIV tag where you want to print the schedule, and then specify the area you want to print, and finally call Window.print to print the specified content.

Sample code (some of the content in the code has been omitted)

Copy Code code as follows:

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 ();
}

The above is the JavaScript code, the following is the HTML page code
Copy Code code as follows:

<div class= "Print" >
<input type= "button" value= "Print Timetable" onclick= "preview ()"/>
</div>
<div class= "Result" >
Schedule Query Results
</div>
<center> This section is printed below </center>
<!--startprint-->
<div class= "Timetable" >
<table id= "table1" class = "Tableresult" style= "Margin-left:auto;margin-right:auto"; >
<tr >
<th> </th>
<th> Monday </th>
<th> Tuesday </th>
<th> Wednesday </th>
<th> Thursday </th>
<th> Friday </th>
<th> Saturday </th>
<th> Sunday </th>
</tr>
<tr >
<th> 1, Section 2 </th>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
<td></td>
</tr>
<tr >
<th> 3, Section 4 </th>
<td></td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
<td></td>
</tr>
<tr >
<th> 5, section 6 </th>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td> Information Technology <br> Digital Letters College <br>501 computer room <br> Bio-professional </td>
</tr>
<tr >
<th> 7, Section 8 </th>
<td></td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
University of <td> Information Technology <br> Digital College <br>501 computer room <br> Bio-professional </td>
<td></td>
<td></td>
</tr>
</table>
</div>
<!--endprint-->
<center> This section is printed above </center>

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.