js調(diào)用iframe實現(xiàn)打印頁面內(nèi)容的方法
1、程序說明
1) 此程序可以實現(xiàn)選擇頁面中的區(qū)域進行打印,以iframe方式進行打??;
2) 與原生態(tài)的print() 區(qū)別在于,取消打印頁面后可以完整保留當(dāng)前訪問頁面的內(nèi)容。
2、代碼部分
1) JS 函數(shù):
function do_print(id_str)//id-str 打印區(qū)域的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;
// 引入打印的專有CSS樣式,www.111Cn.net根據(jù)實際修改
doc.write("<LINK rel="stylesheet" type="text/css" href="css/print.css">");
doc.write('<div>' + el.innerHTML + '</div>');
doc.close();
iframe.contentWindow.focus();
iframe.contentWindow.print();
if (navigator.userAgent.indexOf("MSIE") > 0)
{
document.body.removeChild(iframe);
}
}
2) HTML:
// 打印區(qū)域:
<div id="print_box">
......
</div>
// 調(diào)用打印
<button onclick="javascript:do_print('print_box');">打印</button>
3. 測試
點擊頁面上的打印按鈕,即可測試打??;
除了上面方法我們還可以通過jquery來實例,代碼如下
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.PrintArea.js"></script>
<script>
$(document).ready(function(){
$("input#biuuu_button").click(function(){
$("div#myPrintArea").printArea();
});
});
</script>
<input id="biuuu_button" type="button" value="打印"></input>
<div id="myPrintArea">.....文本打印部分.....</div>
如果要實現(xiàn)區(qū)域打印我們可嘗試下面方法
下面本文分享一種超簡單的方法實現(xiàn)頁面的打印功能,不僅可以打印整個頁面,還可以打印頁面某塊區(qū)域
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
function printdiv(printpage){
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;
}
</script>
<title>div print</title>
</head>
<body>
<input type="button" onClick="printdiv('div_print');" value=" 打印 ">
<div id="div_print">
<h1 style="Color:Red">被打印區(qū)域:www.dhdzp.com</h1>
</div>
這塊區(qū)域是打印不到的!
</body>
</html>
相關(guān)文章
jquery實現(xiàn)獲取具體時間(年月日)后3個月+1天的年月日
這篇文章主要為大家詳細(xì)介紹了jquery如何實現(xiàn)獲取具體時間(年月日)后3個月+1天的年月日,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2023-11-11
JS中批量給元素綁定事件過程中的相關(guān)問題使用閉包解決
解決元素批量綁定事件的時候,出現(xiàn)i=最后一個循環(huán)變量的值的方法有兩種:把這個循環(huán)變量保存起來,不要讓它的作用域在整個函數(shù),而是在循環(huán)體內(nèi)2013-04-04
JQuery學(xué)習(xí)筆記 nt-child的使用
在使用JQuery的時候如果你想尋找某個容器(諸如div或者是table中的某些子元素),那么很容易就使用find方法。2011-01-01
為jQuery-easyui的tab組件添加右鍵菜單功能的簡單實例
下面小編就為大家?guī)硪黄獮閖Query-easyui的tab組件添加右鍵菜單功能的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10

