javascript 局部頁(yè)面打印實(shí)現(xiàn)代碼
更新時(shí)間:2009年08月09日 01:13:36 作者:
Web打印有很多方式,水晶報(bào)表使用的恐怕比較多,但這東西是收費(fèi)軟件,老板說(shuō)不能用:(。
ie自帶的有Active控件,但火狐不支持。這里使用的是js操作dom方法對(duì)窗體指定標(biāo)記內(nèi)文字進(jìn)行打印,所以使用時(shí)需要定義相關(guān)的標(biāo)簽及其樣式(文字大小、字體之類(lèi))。

<script type="text/javascript">
///***********************
///打印指定區(qū)域頁(yè)面
///說(shuō)明:obj–通過(guò)getElementById或其它方式獲取標(biāo)簽標(biāo)識(shí),打印此obj內(nèi)的文字
///日期:2009-8-7
function startPrint(obj)
{
var oWin=window.open("","_blank");
var strPrint="<h4 style='font-size:18px; text-align:center;'>打印預(yù)覽區(qū)</h4>\n";
strPrint=strPrint + "<script type=\"text/javascript\">\n";
strPrint=strPrint + "function printWin()\n";
strPrint=strPrint + "{";
strPrint=strPrint + "var oWin=window.open(\"\",\"_blank\");\n";
strPrint=strPrint + "oWin.document.write(document.getElementById(\"content\").innerHTML);\n";
strPrint=strPrint + "oWin.focus();\n";
strPrint=strPrint + "oWin.document.close();\n";
strPrint=strPrint + "oWin.print()\n";
strPrint=strPrint + "oWin.close()\n";
strPrint=strPrint + "}\n";
strPrint=strPrint + "<\/script>\n";
strPrint=strPrint + "<hr size='1′ />\n";
strPrint=strPrint + "<div id=\"content\">\n";
strPrint=strPrint + obj.innerHTML + "\n";
strPrint=strPrint + "</div>\n";
strPrint=strPrint + "<hr size='1′ />\n";
strPrint=strPrint + "<div style='text-align:center'><button onclick='printWin()' style='padding-left:4px;padding-right:4px;'>打 印</button><button onclick='window.opener=null;window.close();' style='padding-left:4px;padding-right:4px;'>關(guān) 閉</button></div>\n";
oWin.document.write(strPrint);
oWin.focus();
oWin.document.close();
}
</script>
<button id="btnPrint" onclick="startPrint(document.getElementById('content'))">打印內(nèi)容</button>
<div id="content">
<div style="font-size:12px;color:#333;">
這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容
</div>
</div>
下面是測(cè)試代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]

復(fù)制代碼 代碼如下:
<script type="text/javascript">
///***********************
///打印指定區(qū)域頁(yè)面
///說(shuō)明:obj–通過(guò)getElementById或其它方式獲取標(biāo)簽標(biāo)識(shí),打印此obj內(nèi)的文字
///日期:2009-8-7
function startPrint(obj)
{
var oWin=window.open("","_blank");
var strPrint="<h4 style='font-size:18px; text-align:center;'>打印預(yù)覽區(qū)</h4>\n";
strPrint=strPrint + "<script type=\"text/javascript\">\n";
strPrint=strPrint + "function printWin()\n";
strPrint=strPrint + "{";
strPrint=strPrint + "var oWin=window.open(\"\",\"_blank\");\n";
strPrint=strPrint + "oWin.document.write(document.getElementById(\"content\").innerHTML);\n";
strPrint=strPrint + "oWin.focus();\n";
strPrint=strPrint + "oWin.document.close();\n";
strPrint=strPrint + "oWin.print()\n";
strPrint=strPrint + "oWin.close()\n";
strPrint=strPrint + "}\n";
strPrint=strPrint + "<\/script>\n";
strPrint=strPrint + "<hr size='1′ />\n";
strPrint=strPrint + "<div id=\"content\">\n";
strPrint=strPrint + obj.innerHTML + "\n";
strPrint=strPrint + "</div>\n";
strPrint=strPrint + "<hr size='1′ />\n";
strPrint=strPrint + "<div style='text-align:center'><button onclick='printWin()' style='padding-left:4px;padding-right:4px;'>打 印</button><button onclick='window.opener=null;window.close();' style='padding-left:4px;padding-right:4px;'>關(guān) 閉</button></div>\n";
oWin.document.write(strPrint);
oWin.focus();
oWin.document.close();
}
</script>
<button id="btnPrint" onclick="startPrint(document.getElementById('content'))">打印內(nèi)容</button>
<div id="content">
<div style="font-size:12px;color:#333;">
這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容這里是打印內(nèi)容
</div>
</div>
下面是測(cè)試代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
相關(guān)文章
JS實(shí)現(xiàn)的N多簡(jiǎn)單無(wú)縫滾動(dòng)代碼(包含圖文效果)
這篇文章主要介紹了JS實(shí)現(xiàn)的N多簡(jiǎn)單無(wú)縫滾動(dòng)代碼,包含了文字及圖文等多種滾動(dòng)效果,涉及JavaScript遞歸調(diào)用及定時(shí)函數(shù)觸發(fā)實(shí)現(xiàn)頁(yè)面元素動(dòng)態(tài)變換的相關(guān)技巧,需要的朋友可以參考下2015-11-11基于JavaScript實(shí)現(xiàn) 網(wǎng)頁(yè)切出 網(wǎng)站title變化代碼
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn) 網(wǎng)頁(yè)切出 網(wǎng)站title變化代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04ie6下png圖片背景不透明的解決辦法使用js實(shí)現(xiàn)
我們時(shí)常在使用png圖片的時(shí)候,在ie6下發(fā)生背景不透明的問(wèn)題,解決的方法實(shí)在是太多了,下面給大家介紹下一個(gè)js解決的方式,感興趣的朋友可以了解下的2013-01-01非常不錯(cuò)的不間斷循環(huán)滾動(dòng)類(lèi) 兼容多瀏覽器
非常不錯(cuò)的不間斷循環(huán)滾動(dòng)類(lèi) 兼容多瀏覽器...2006-12-12JavaScript圣杯布局與雙飛翼布局實(shí)現(xiàn)案例詳解
這篇文章主要介紹了JavaScript圣杯布局與雙飛翼布局實(shí)現(xiàn)案例,這是前端面試中需要掌握的知識(shí)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08JavaScript初學(xué)者需要了解10個(gè)小技巧
在之前的編程語(yǔ)言排行榜中,我們?cè)榻B過(guò)轉(zhuǎn)正在即的JavaScript語(yǔ)言,正如文章中闡明的那樣,JavaScript不僅是最具活力的腳本語(yǔ)言,還是是最有用的編程語(yǔ)言之一。2010-08-08javascript 禁用IE工具欄,導(dǎo)航欄等等實(shí)現(xiàn)代碼
在處理問(wèn)題時(shí)候遇到的,就順便記錄與大家一起分享下,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04