<html>
<head></head>
<style type="text/css" media="screen">
@media print{
.print {display:block;}
.notPrint {display:none;}
}
</style>
<script language="javascript">
function preview()
{
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>
<body>
<form id="WebForm1" method="post" >
<center>本部分以上不被打印</center>
<!--startprint-->
<div align="center">
<asp:DataGrid id="dgShow" runat="server">
<!--省略部分代碼-->
</asp:DataGrid>
<table>
<tr><td>打印打印打印打印打印打印打印打印</td></tr>
</table>
</div>
<!--endprint-->
<center>本部分以下不被打印</center>
<div align="center">
<input type="button" name="print" value="預覽并打印" onclick="preview()">
</div>
<style> @media Print { .Noprn { DISPLAY: none }}
</style>
<p class="Noprn">不打印</p>
<table id="datagrid">
<tr>
<td>打印</td>
</tr>
</table>
<input class="Noprn" type="button" onclick="window.print()" value="print">
</form>
</body>
</html>
-------------
使用JS打印,
看代碼
<script language="javascript">
function preview()
{
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>
這段代碼,他是打印
<!--startprint-->和<!--endprint-->
這個標記之間的網頁內容。
所以網頁中要包含這個2個標簽才行???
下面是去除頁腳頁眉的js代碼
<script>
var HKEY_Root,HKEY_Path,HKEY_Key;
HKEY_Root="HKEY_CURRENT_USER";
HKEY_Path="http://Software//Microsoft//Internet Explorer//PageSetup//";
//設置網頁打印的頁眉頁腳為空
function PageSetup_Null()
{
try
{
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
}
catch(e)
{}
}
//設置網頁打印的頁眉頁腳為默認值
function PageSetup_Default()
{
try
{
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b頁碼,&p/&P");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");
}
catch(e)
{}
}
PageSetup_Default();
</script>
注意有可能執行時,會出現沒有效果的錯誤,這時原因是可能你的瀏覽器限制了active對象的創建,只要取消限制就好了,取消方法如下:
打開你的ie瀏覽器internet選項―― 安全―― 自定義級別―― 把對沒有標記為安全的activex控件進行初始化和腳本運行 設置為啟用
更多功能請參考武林網現在用的代碼。
http://img.VeVB.COm/inc/content.js