JS 實現簡單的頁面局部打印


JS實現局部打印和預覽:
第一種:
JS 實現簡單的頁面局部打印

function preview(oper)
{
if (oper < 10)...{
bdhtml=window.document.body.innerHTML;//獲取當前頁的html代碼
sprnstr="<!--startprint"+oper+"-->";//設置打印開始區域
eprnstr="<!--endprint"+oper+"-->";//設置打印結束區域
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;


} else {
window.print();
}

}

 

使用很簡單 將頁面內要打印的內容加入中間<!--startprint1-->XXXXX<!--endprint1-->
再加個打印按紐 onclick=preview(1)

第二中:
下面就是實現局部打印的代碼,跟大家分享一下,希望能夠對大家有所幫助。

function Printpart(id_str)//id-str 內容中的id
{
var el = document.getElementById(id_str);
var iframe = document.createElement('IFRAME');
var doc = null;
iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
doc.write('<div>' + el.innerHTML + '</div>');
doc.close();
iframe.contentWindow.focus();
iframe.contentWindow.print();
if (navigator.userAgent.indexOf("MSIE") > 0)
{
document.body.removeChild(iframe);
}
}

 

將要想打印局部內容,將包含內容的標簽ID 當參數放入函數就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM