php实现打印预览功能的方法:首先获取当前页的html代码;然后设置打印开始区域和打印结束区域;接着从开始和结束代码向后取html;最好确定要打印的内容即可。

推荐:《PHP视频教程》
php 实现打印预览的功能
<inputid="btnPrint" type="button" value="打印"onclick="javascript:window.print();" />
<inputid="btnPrint" type="button" value="打印预览" onclick=preview(1)/>
<styletype="text/css" media=print>
.noprint{display : none}
</style>
<pclass="noprint">不须要打印的地方</p>
<script>
functionpreview(oper)
{
if (oper <10)
{
bdhtml=window.document.body.innerHTML;//获取当前页的html代码
sprnstr="<!–startprint"+oper+"–>";//设置打印開始区域
eprnstr="<!–endprint"+oper+"–>";//设置打印结束区域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18);//从開始代码向后取html
prnhtmlprnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
</script>
<p>XXXXX</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/xiazai/code/9656">
<img src="https://img.php.cn/upload/webcode/000/000/013/175957920695254.png" alt="B2S商城系统">
</a>
<div class="aritcle_card_info">
<a href="/xiazai/code/9656">B2S商城系统</a>
<p>B2S商城系统B2S商城系统是由佳弗网络工作室凭借专业的技术、丰富的电子商务经验在第一时刻为最流行的分享式购物(或体验式购物)推出的开源程序。开发采用PHP+MYSQL数据库,独立编译模板、代码简洁、自由修改、安全高效、数据缓存等技术的应用,使其能在大浏览量的环境下快速稳定运行,切实节约网站成本,提升形象。注意:如果安装后页面打开出现找不到数据库等错误,请删除admin下的runtime文件夹和a</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="B2S商城系统">
<span>0</span>
</div>
</div>
<a href="/xiazai/code/9656" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="B2S商城系统">
</a>
</div>
<!–startprint1–>要打印的内容<!–endprint1–>
<inputid="btnPrint" type="button" value="打印"onclick="javascript:window.print();" />
<inputid="btnPrint" type="button" value="打印预览" onclick=preview(1)/>
<styletype="text/css" media=print>
.noprint{display : none}
</style>
<pclass="noprint">不须要打印的地方</p>
<script>
functionpreview(oper)
{
if (oper <10)
{
bdhtml=window.document.body.innerHTML;//获取当前页的html代码
sprnstr="<!–startprint"+oper+"–>";//设置打印開始区域
eprnstr="<!–endprint"+oper+"–>";//设置打印结束区域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18);//从開始代码向后取html
prnhtmlprnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
</script>
<p>XXXXX</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<!–startprint1–>要打印的内容<!–endprint1–>以上就是php如何实现打印预览功能的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号