打印时隐藏页面部分内容

本文介绍如何使用HTML与JavaScript实现网页的打印功能,包括隐藏特定元素、制作打印按钮并使其随页面滚动显示。通过简单的代码示例,展示了如何利用浏览器内置的打印功能。
<STYLE media="print">
#btnprint,#printdiv {
display: none
}
</STYLE>
使用<style>标签的media属性。详见 http://www.w3school.com.cn/tags/att_style_media.asp

打印按钮:
<div id="printdiv"
style="position: absolute; width: 100px; height: 33px; top: 10px; left: 660px; background-color: orange; text-align: center; border: 1px solid black">
<input type=button id=btnprint value="【打印】" style="margin-top: 5px"
onclick="doPrint()">
</div>
function doPrint(){
window.print(); 
}
使【打印】按钮随页面滚动:
function movePrintBtn(){
document.all.printdiv.style.top = document.body.scrollTop + 10 + "px";
}
使用onscroll。 HTML 5 事件属性: http://www.w3school.com.cn/html5/html5_ref_eventattributes.asp
<body onscroll="movePrintBtn()">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值