有關網頁打印,可以使用的方法有:“1.點擊鼠標右鍵,選擇打印預覽,執行打印;2.按鍵盤上的[print sc sysrq]按鈕”;還有打印當前網頁的方法就是,用相關代碼來實現,這裏用到的是js;其實用js打印網頁很簡單,最爲重要的就是print()這個函數。
但是簡單也不要掉以輕心,不同地方用到,處理方式也需要注意,不然也會得到一些自己不想的結果。
1.打印整個網頁的內容:
window.print();
2.打印指定id/class的網頁內容:
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr+footstr;
window.print();
document.body.innerHTML = oldstr;
return false;
● 以上方法在本頁面有表單時,可能會對數據完整性造成影響,換成下面的方法代替可解決這個問題:
方案一:
var divToPrint = document.getElementById("myMreview");
newWin = window.open();
newWin.document.write(divToPrint.innerHTML);
newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();
方案二:
bdhtml=window.document.body.innerHTML;//獲取當前頁的html代碼
sprnstr="<!--startprint-->";//設置打印開始區域
eprnstr="<!--endprint-->";//設置打印結束區域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //從開始代碼向後取html
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//從結束代碼向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
>>原因 是因爲,前者是在本頁面內容替換,相關的數據可能被替換掉了,再來找就找不到了;後者是在新的頁面進行打印操作,不影響前一個頁面數據。
3.可以使用jquery插件:
PrintArea
jqprint
Fingerprint
printpage
Lodop
等等...