100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 用js实现页面打印以及自定义打印内容

用js实现页面打印以及自定义打印内容

时间:2019-11-26 03:51:19

相关推荐

用js实现页面打印以及自定义打印内容

实现一:

<input type="button" value="打印" onClick="document.execCommand('print')"/> <!--将不需要打印的部分,标记为 class="noprint" --> <style type="text/css" media=print> .noprint{display : none } </style> <p class="noprint">不需要打印的地方</p> <p>打印的部分</p>

实现二:WebBrowser控件

<object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object> <input type="button" value="打印" onClick="WebBrowser.ExecWB(8,1)"/>

实现三:windows自带的打印对象

<input type="button" value="打印"onClick="window.print()"/>

实现四:jQuery实现--支持局部打印

<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#button").click(function(){ $("div#PrintArea").printArea(); }); }); </script> <input id="button" type="button" value="打印"/><div id="PrintArea">文本打印部分</div>

===================下面为jquery.PrintArea.js插件的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);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。