日期:2014-05-16  浏览次数:20392 次

JS实现网页的简单打印
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
	打印审批页面
</title>
 <style type="text/css">
#dysp
{
	margin:0px;
	padding:10px;
	overflow:auto;
	color:#000;
	width:760px;
}
#dysp h1
{
	text-align:center;
}
#dysp p.sp1
{
	padding:0px;
	margin:2px 0px;
	text-align:left;
}
#dysp table
{
	border:solid 1px #000;
	border-collapse:collapse;
	margin-bottom:10px;
}
#dysp table td
{
	border:solid 1px #000;
	text-align:left;
}

#dysp table th
{
	padding:5px 0px;
	border:solid 1px #000;
}
</style>
<script language="Javascript" type="text/javascript"> 
function preview(index)
{
     bdhtml=window.document.body.innerHTML;
            sprnstr="<!--startprint-->";
            eprnstr="<!--endprint-->";
            prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
            prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
            window.document.body.innerHTML=prnhtml;
            window.print(); 
}
</script>
</head>
<body>
<div id="container">                         
<!--startprint-->
<div id="dysp">
    <table>
        <tr>
            <th class="qsTime">起始时间</th>
            <th class="jsTime">结束时间</th>
            <th class="jbxx">工作内容</th>
            <th class="bz">备注</th>
        </tr>        
        <tr>
            <td>2009-05-14</td>
            <td>2009-05-14</td>
            <td>aaaaaaaaaaaaaaaaaa </td>
            <td></td>            
        </tr>  
	<tr>
            <td>2009-05-14</td>
            <td>2009-05-14</td>
            <td>aaaaaaaaaaaaaaaaaa </td>
            <td></td>            
        </tr> 
	<tr>
            <td>2009-05-14</td>
            <td>2009-05-14</td>
            <td>aaaaaaaaaaaaaaaaaa </td>
            <td></td>            
        </tr>   
	<tr>
            <td>2009-05-14</td>
            <td>2009-05-14</td>
            <td>aaaaaaaaaaaaaaaaaa </td>
            <td></td>            
        </tr> 
	<tr>
            <td>2009-05-14</td>
            <td>2009-05-14</td>
            <td>aaaaaaaaaaaaaaaaaa </td>
            <td></td>            
        </tr>               
    <!--endprint-->
   
    <p><input id="Button1" type="button" value="打 印" class="btnDysp" 
    onclick="preview(0)"/></p>
</div>
    </div>
    </form>
</body>
</html>