Home >Web Front-end >JS Tutorial >js calls iframe to implement the method of printing page content_jquery

js calls iframe to implement the method of printing page content_jquery

WBOY
WBOYOriginal
2016-05-16 16:57:101771browse

1. 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:

Copy code The code is as follows:

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("");
doc.write('
' el.innerHTML '
');
doc.close();
iframe.contentWindow .focus();
iframe.contentWindow.print();
if (navigator.userAgent.indexOf("MSIE") > 0)
{
document.body.removeChild(iframe) ;
}
}

2) HTML:

Copy code The code is as follows:

//Print area:

// Call print



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:

Copy the code The code is as follows:



<script><br>$(document).ready(function(){<br> $("input#biuuu_button").click(function( ){ <p> $("div#myPrintArea").printArea();</p> <p>});<br>});<br></script>

.....Text printing part....

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

Copy code The code is as follows:





div print



Print area: www.jb51.net



This area cannot be printed!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn