1. Program description
1) This program can select an area on the page for printing and print in iframe mode;
2) The difference from the original print() is that the content of the currently accessed page can be completely retained after canceling the printing page.
2. Code part
1) JS function:
Copy the code code as follows:
function do_print(id_str)//id-str prints the id of the area
{
var el = document.getElementById(id_str);
var iframe = document.createElement('IFRAME');
vardoc = null;
iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
//Introduce the proprietary CSS style for printing, www.111Cn.net will modify 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:
Copy the code code as follows:
//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 methods, we can also use jquery to instantiate, the code is as follows
Copy the code code 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 we 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.
Copy the code code as follows:
<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.VeVB.COm</h1>
</div>
This area cannot be printed!
</body>
</html>