Window. print a method that specifies a div to specify a region on a web page

  • 2020-03-30 03:39:24
  • OfStack

The first method: specify a do not print area
Using CSS, define a class for.noprint, and put the contents that are not printed into the class.
Fine as follows:


<style media=print type="text/css"> 
.noprint{visibility:hidden} 
</style>

What to print. Ha ha!


<p class="noprint"> Put the code that is not printed here. </p> 
<a href="javascript:window.print()" rel="external nofollow" target="_self"> print </a>

The second way: specify the print area
Put what you want to print into a span or div, and then print it with a function.


<span id='div1'> Put the print in here </span> 
<p> All content </p> 
<div id="div2">div2 The content of the </div> 
<a href="javascript:printme()" rel="external nofollow" target="_self"> print </a> 
<script language="javascript"> 
function printme() 
{ document.body.innerHTML=document.getElementByIdx_x_x('div1').innerHTML+'<br/>'+document.getElementByIdx_x_x('div2').innerHTML; 
window.print(); 
} 
</script>

If you want to print only a small part of the entire page, it is best to use the second method.

First we can put what we want to print in a div and then print it with the following code.


<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>

Related articles: