Print jquery code in some areas of the webpage

Source: Internet
Author: User

The Code is also found on the network and is now recorded

 1 (function ($) { 2     var printAreaCount = 0; 3     $.fn.printArea = function () { 4         var ele = $(this); 5         var idPrefix = "printArea_"; 6         removePrintArea(idPrefix + printAreaCount); 7         printAreaCount++; 8         var iframeId = idPrefix + printAreaCount; 9         var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';10         iframe = document.createElement('IFRAME');11         $(iframe).attr({12             style: iframeStyle,13             id: iframeId14         });15         document.body.appendChild(iframe);16         var doc = iframe.contentWindow.document;17         $(document).find("link").filter(function () {18             return $(this).attr("rel").toLowerCase() == "stylesheet";19         }).each(20                 function () {21                     doc.write('<link type="text/css" rel="stylesheet" href="'22                             + $(this).attr("href") + '" >');23                 });24         doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html()25                 + '</div>');26         doc.close();27         var frameWindow = iframe.contentWindow;28         frameWindow.close();29         frameWindow.focus();30         frameWindow.print();31     }32     var removePrintArea = function (id) {33         $("iframe#" + id).remove();34     };35 })(jQuery);

Call on the page

To print the following div

 

1 <div class="imgCnt">2 …………3 </div>

 

 

Add the following code to the js part of the page.

$(".imgCnt").printArea();

 

 

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.