


js calls iframe to implement the method of printing page content_jquery
May 16, 2016 pm 04:57 PM1. Program description
1) This program can select an area on the page to print and print in iframe mode;
2) The difference from the original print() is that it cancels printing The content of the currently visited page can be completely retained after the page.
2. Code part
1) JS function:
function do_print(id_str)//id-str print area id
{
var el = document.getElementById(id_str);
var iframe = document.createElement('IFRAME');
var doc = null;
iframe.setAttribute(' style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
document.body.appendChild(iframe);
doc = iframe.contentWindow. document;
//Introducing the proprietary CSS style for printing, www.111Cn.net modifies it according to the actual situation
doc.write("<LINK rel="stylesheet" type="text/css" href="css /print.css">");
doc.write('<div>' el.innerHTML '</div>');
doc.close();
iframe.contentWindow .focus();
iframe.contentWindow.print();
if (navigator.userAgent.indexOf("MSIE") > 0)
{
document.body.removeChild(iframe) ;
}
}
2) HTML:
//Print area:
<div id="print_box">
......
</div>
// Call print
<button onclick="javascript:do_print( 'print_box');">Print</button>
3. Test
Click the print button on the page to test printing;
In addition to the above method, we can also use jquery to implement the example. The code is as follows:
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src=" jquery.PrintArea.js"></script>
<script>
$(document).ready(function(){
$("input#biuuu_button").click(function( ){
$("div#myPrintArea").printArea();
});
});
</script>
<input id="biuuu_button" type="button" value="Print"></input>
<div id="myPrintArea">.....Text printing part....</div>
If you want to achieve area printing, we can try the following method
The following article shares a super simple method to realize the printing function of the page. It can not only print the entire page, but also print a certain area of the page
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
function printdiv(printpage ){
var headstr="<html><head><title></title></head><body>";
var footstr="</body>" ;
var newstr=document.all.item(printpage).innerHTML;
var oldstr=document.body.innerHTML;
document.body.innerHTML=headstr newstr footstr;
window.print( );
document.body.innerHTML=oldstr;
return false;
}
</script>
<title>div print</title>
</ head>
<body>
<input type="button" onClick="printdiv('div_print');" value=" print">
<div id="div_print"> ;
<h1 style="Color:Red">Print area: www.jb51.net</h1>
</div>
This area cannot be printed!
</body>
</html>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Maps to implement map pan function

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

Recommended: Excellent JS open source face detection and recognition project

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function
