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

<script type="text/javascript">
///***********************
///打印指定區(qū)域頁面
///說明:obj–通過getElementById或其它方式獲取標簽標識,打印此obj內(nèi)的文字
///日期:2009-8-7
function startPrint(obj)
{
var oWin=window.open("","_blank");
var strPrint="<h4 style='font-size:18px; text-align:center;'>打印預覽區(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>
下面是測試代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]

復制代碼 代碼如下:
<script type="text/javascript">
///***********************
///打印指定區(qū)域頁面
///說明:obj–通過getElementById或其它方式獲取標簽標識,打印此obj內(nèi)的文字
///日期:2009-8-7
function startPrint(obj)
{
var oWin=window.open("","_blank");
var strPrint="<h4 style='font-size:18px; text-align:center;'>打印預覽區(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>
下面是測試代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
相關(guān)文章
基于JavaScript實現(xiàn) 網(wǎng)頁切出 網(wǎng)站title變化代碼
這篇文章主要介紹了基于JavaScript實現(xiàn) 網(wǎng)頁切出 網(wǎng)站title變化代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04ie6下png圖片背景不透明的解決辦法使用js實現(xiàn)
我們時常在使用png圖片的時候,在ie6下發(fā)生背景不透明的問題,解決的方法實在是太多了,下面給大家介紹下一個js解決的方式,感興趣的朋友可以了解下的2013-01-01JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解
這篇文章主要介紹了JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例,這是前端面試中需要掌握的知識點,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08javascript 禁用IE工具欄,導航欄等等實現(xiàn)代碼
在處理問題時候遇到的,就順便記錄與大家一起分享下,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04