在页面中引入
<script>
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 =document.getElementById("mainbody").innerHTML;
//window.print();
window.document.body.innerHTML = prnhtml;
window.print();
window.document.body.innerHTML = bdhtml;
}
</script>
在需要打印的地方,比如table
<!--startprint-->
<table>
</table>
<!--endprint-->
<input type="button" onclick="doPrint()" value="打印"/>
就会打印table中的内容
本文介绍了一种使用JavaScript实现网页局部内容打印的方法。通过定义开始打印和结束打印的注释标签,可以精确控制打印区域,避免打印整个页面内容。此方法适用于需要精确定位打印范围的场景。
1281

被折叠的 条评论
为什么被折叠?



