做項目時,有在網(wǎng)頁實現(xiàn)全局和局部打印的需求,百度許久,現(xiàn)總結(jié)如下:
打印方式一:
1.首先在head里面加入下面一段js代碼:
function preview(fang)
{
if (fang < 10){
bdhtml=window.document.body.innerHTML;//獲取當前頁的html代碼
sprnstr="<!--startprint"+fang+"-->";//設置打印開始區(qū)域
eprnstr="<!--endprint"+fang+"-->";//設置打印結(jié)束區(qū)域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //從開始代碼向后取html
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//從結(jié)束代碼向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
2.然后在所需要打印的代碼,用和包圍著,如下:
<!--startprint1-->
<!--打印內(nèi)容開始-->
<!-- <div id=wdf>
...
</div>
打印內(nèi)容結(jié)束-->
<!--endprint1-->
3.最后加上一個打印的按鈕
<input type='button' name='button_export' title='打印1' οnclick=preview(1) value='打印1'>
打印方式二:
1.javascript中方法為:
function dayin(){
var userAgent = navigator.userAgent.toLowerCase(); //取得瀏覽器的userAgent字符串
if (userAgent.indexOf("trident") > -1){
alert("請使用google或者360瀏覽器打印");
return false;
}else if(userAgent.indexOf('msie')>-1){
var onlyChoseAlert = simpleAlert({
"content":"請使用Google或者360瀏覽器打印",
"buttons":{
"確定":function () {
onlyChoseAlert.close();
}
}
})
alert("請使用google或者360瀏覽器打印");
return false;
}else{//其它瀏覽器使用lodop
var oldstr = document.body.innerHTML;
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
//執(zhí)行隱藏打印區(qū)域不需要打印的內(nèi)容
document.getElementById("otherpho").style.display="none";
//此處id換為你自己的id
var printData = document.getElementById("printdivaa").innerHTML; //獲得 div 里的所有 html 數(shù)據(jù)
document.body.innerHTML = headstr+printData+footstr;
window.print();
//打印結(jié)束后,放開隱藏內(nèi)容
document.getElementById("otherpho").style.display="block";
document.body.innerHTML = oldstr ;
}
}
2.頁面內(nèi)容如下:
<div id='printdivaa'>
...
</div>
<div id="otherpho"></div>
3.頁面中放置一個打印按鈕:
<button type="button" class="btn_search" onclick="dayin()">打印</button>
打印方式三(此方式會重新打開一個瀏覽器窗口):
1.javascript中方法為:
function dayin() {
var userAgent = navigator.userAgent.toLowerCase(); //取得瀏覽器的userAgent字符串
if (userAgent.indexOf("trident") > -1) {
alert("請使用google或者360瀏覽器打印");
return false;
} else if (userAgent.indexOf('msie') > -1) {
var onlyChoseAlert = simpleAlert({
"content" : "請使用Google或者360瀏覽器打印",
"buttons" : {
"確定" : function() {
onlyChoseAlert.close();
}
}
})
alert("請使用google或者360瀏覽器打印");
return false;
} else {//其它瀏覽器使用lodop
var oldstr = document.body.innerHTML;
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body></html>";
//執(zhí)行隱藏打印區(qū)域不需要打印的內(nèi)容
document.getElementById("otherpho").style.display="none";
var printData = document.getElementById("studentPhoTable").innerHTML; //獲得 div 里的所有 html 數(shù)據(jù)
var wind = window.open("", "newwin",
"toolbar=no,scrollbars=yes,menubar=no");
wind.document.body.innerHTML = headstr + printData + footstr;
wind.print();
//打印結(jié)束后,放開隱藏內(nèi)容
document.getElementById("otherpho").style.display="block";
wind.close();
window.document.body.innerHTML = oldstr;
}
}
2.頁面內(nèi)容如下:
<div id='studentPhoTable'>
...
</div>
3.頁面中放置一個打印按鈕:
<input type="button" οnclick="print()" value="確定打印" />