局部打印插件 jquery.PrintArea.js


(function ($) { var printAreaCount = 0; $.fn.printArea = function () { var ele = $(this); var idPrefix = "printArea_"; removePrintArea(idPrefix + printAreaCount); printAreaCount++; var iframeId = idPrefix + printAreaCount; var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;'; iframe = document.createElement('IFRAME'); $(iframe).attr({ style: iframeStyle, id: iframeId }); document.body.appendChild(iframe); var doc = iframe.contentWindow.document; $(document).find("link").filter(function () { return $(this).attr("rel").toLowerCase() == "stylesheet"; }).each( function () { doc.write('<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >'); }); doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div>'); doc.close(); var frameWindow = iframe.contentWindow; frameWindow.close(); frameWindow.focus(); frameWindow.print(); } var removePrintArea = function (id) { $("iframe#" + id).remove(); }; })(jQuery);


PrintArea.js ----- ---- ---- 源码
<!DOCTYPE html>

<html> <head> <meta charset="utf-8"> <script type="text/javascript" src="../js/jquery-2.1.4.js"></script> <script type="text/javascript" src="../js/PrintArea.js"></script> <script> $(function() { $("input#biuuu_button").click(function() { $("div#myPrintArea").printArea(); }); }); </script> </head> <body> <input id="biuuu_button" type="button" value="打印"></input> <div id="myPrintArea">.....文本打印部分.....</div> <div class="quote_title">引用</div> <div class="quote_div"></div> </body> </html>

 

posted @ 2015-11-26 14:55  czhyuwj  阅读(518)  评论(0编辑  收藏  举报