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

推荐:《PHP视频教程》
php 实现打印预览的功能
<inputid></inputid><inputid></inputid><styletype>
.noprint{display : none}
<pclass>不须要打印的地方
<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><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
要打印的内容
<inputid></inputid><inputid></inputid><styletype>
.noprint{display : none}
<pclass>不须要打印的地方
<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>
要打印的内容</pclass></styletype></pclass></styletype>
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










