只要在html的button上寫onclick="print()"
EX:
<input type="button" name="btn1" value="列印" title="列印" onclick="print()">
部分列印(只列印div包起來的網頁):
html部分:
<div id="divs">
<P>adfdsbsdfds</P>
</div>
<input type="button" value="部分列印" onclick="printS(divs)">
Javascript部分:
//列印div包起來的部分並且列印完畢後自動關閉列印網頁
function printS(divs){
var value = divs.innerHTML;
var printPage = window.open("","printPage","");
printPage.document.open();
printPage.document.write("<HTML><head></head><BODY onload='window.print();window.close()'>");
printPage.document.write(value);
printPage.document.close("</BODY></HTML>");
}
來源網址:http://nievor.com/2008/07/31/javascript_print_preview/