实现一:
<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);