window.print打印指定div指定⽹页指定区域的⽅法
第⼀种⽅法:指定不打印区域
使⽤CSS,定义⼀个.noprint的class,将不打印的内容放⼊这个class内。
细如下:
<style media=print type="text/css">
.noprint{visibility:hidden}
</style>
要打印的内容。哈哈!
<p class="noprint">将不打印的代码放在这⾥。</p>
<a href="javascript:window.print()" rel="external nofollow" target="_self">打印</a>
第⼆种⽅法:指定打印区域
把要打印的内容放⼊⼀个 span或div,然后通过⼀个函数打印。
<span id='div1'>把要打印的内容放这⾥</span>
<p>所有内容</p>
<div id="div2">div2的内容</div>
<a href="javascript:printme()" rel="external nofollow" target="_self">打印</a>
<script language="javascript">
function printme()
{ document.body.ElementByIdx_x_x('div1').innerHTML+'<br/>'+ElementByIdx_x_x('div2').innerHTML; window.print();网页自动关闭
}
</script>
如果要打印的只是整个页⾯中的⼀⼩部分,就最好采⽤第⼆种⽅法。
第三种⽅法:如果要打印的页⾯排版和原web页⾯相差很⼤,采⽤此种⽅法。点打印按钮弹出新窗⼝,把需要打印的内容显⽰到新窗⼝中,在新窗⼝中调⽤window.print()⽅法,然后⾃动关闭新窗⼝。
window.print可以打印⽹页,但有时候我们只希望打印特定控件或内容,怎么办呢?
⾸先我们可以把要打印的内容放在div中,然后⽤下⾯的代码进⾏打印。
<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 >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>