<html>
<OBJECT id="WebBrowser" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"
VIEWASTEXT>
</OBJECT>
<body id="body1">
<div id="1" style="display: block;">asdfsdfsfsfwefwef</div>
<div id="2" style="display: block;">b</div>
<div id="3" style="display: block;">c</div>
<div id="4" style="display: block;">d</div>
<div><input name="" type="button" onClick="q()"></div>
</body>
<script>
var temp = "";
function q(){
window.onbeforeprint = function(){
document.getElementById("1").style.display = "none";
};
window.onafterprint = function(){
document.getElementById("1").style.display = "";
};
window.print();
}
</script>
</html>
本文介绍了一种在网页打印过程中通过JavaScript动态隐藏指定元素的方法。该方法利用了浏览器的onbeforeprint和onafterprint事件来实现在打印前隐藏指定div,并在打印完成后恢复其显示状态。这种技巧适用于那些希望在打印页面时排除某些不必要元素的场景。
131

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



