方法一:
<script language="javascript" type="text/javascript">
function printpage(myDiv){
//var newstr = document.all.item(myDiv).innerHTML;
var newstr = document.getElementById(myDiv).innerHTML;
alert(newstr);
var oldstr = document.body.innerHTML;
document.body.innerHTML = newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
<div id="myDiv" >说明:打印整个div区域的内容,如果包含按钮,则按钮也会打印出来!
<div>
打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容
adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf
</div>
<input type="button" id="bt" onclick="javascript:printpage('myDiv')" value="打印" />
</div>
方法二:
<script language="javascript" type="text/javascript">
function doPrint() {
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>
<div>
<!--startprint-->说明:这是开始打印的位置
打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容
adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf
<!--endprint-->说明:这是结束打印的位置
</div>
<input type="button" id="bt" onclick="javascript:doPrint()" value="打印" />
</div>
用户在打印网页的时候,页面上会出现网页的名字,页码,链接地址和打印时间,如果用户不需要这些信息,则需要设置用户的网页设置:
具体如下:文件-》页面设置-》将页眉页脚中的代码删除即可。打印出来的文档没有页码信息和链接地址信息