js window.print實(shí)現(xiàn)打印特定控件或內(nèi)容
更新時(shí)間:2013年09月16日 09:55:19 作者:
window.print可以打印網(wǎng)頁,但有時(shí)候我們只希望打印特定控件或內(nèi)容,怎么辦呢?可以把要打印的內(nèi)容放在div中,然后用下面的代碼進(jìn)行打印,希望對(duì)大家有所幫助
window.print可以打印網(wǎng)頁,但有時(shí)候我們只希望打印特定控件或內(nèi)容,怎么辦呢?
window.print打印指定div
首先我們可以把要打印的內(nèi)容放在div中,然后用下面的代碼進(jìn)行打印。
<html>
<head>
<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>
//HTML Page
//Other content you wouldn't like to print
<input name="b_print" type="button" class="ipt" onClick="printdiv('div_print');" value=" Print ">
<div id="div_print">
<h1 style="Color:Red">The Div content which you want to print</h1>
</div>
//Other content you wouldn't like to print
//Other content you wouldn't like to print
</body>
</html>
window.print打印指定div
首先我們可以把要打印的內(nèi)容放在div中,然后用下面的代碼進(jìn)行打印。
復(fù)制代碼 代碼如下:
<html>
<head>
<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>
//HTML Page
//Other content you wouldn't like to print
<input name="b_print" type="button" class="ipt" onClick="printdiv('div_print');" value=" Print ">
<div id="div_print">
<h1 style="Color:Red">The Div content which you want to print</h1>
</div>
//Other content you wouldn't like to print
//Other content you wouldn't like to print
</body>
</html>
相關(guān)文章
實(shí)現(xiàn)只能輸入數(shù)字的input不用replace方法
只能輸入數(shù)字在以往都是使用replace方法實(shí)現(xiàn)的,在本文你將學(xué)習(xí)到不使用它依然可以實(shí)現(xiàn),具體代碼如下,感興趣的朋友可以參考下2013-09-09jquery插件bootstrapValidator數(shù)據(jù)驗(yàn)證詳解
這篇文章主要為大家詳細(xì)介紹了jquery插件bootstrapValidator數(shù)據(jù)驗(yàn)證使用教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Javascript attachEvent傳遞參數(shù)的辦法
找了半天找到的解決辦法,看介紹說是javascript的閉包問題,導(dǎo)致得不能直接讀取外部的那個(gè)函數(shù),不然就所有傳遞的參數(shù)都變?yōu)樽詈笠粋€(gè)了。2009-12-12JavaScript數(shù)組排序功能簡單實(shí)現(xiàn)
這篇文章主要介紹了JavaScript數(shù)組排序功能簡單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05