window.print()简单使用

window.print()的使用
整体打印隐藏掉不需要的结构

					document.getElementsByClassName('xmbz')[0].style.display='none';
					document.getElementsByClassName('reason')[0].style.display='none';
					document.getElementById('printBox').style.display='none';//不需要打印的部分隐藏printNav\printerLogo\\printBoxStyle
					document.getElementById('printButton').style.display='none';
					document.getElementById('printNav').style.display='none';
					document.getElementById('printerLogo').style.display='none';

设置打印样式
1、js改变样式

document.getElementById('printBoxStyle').style.width='100%';

2、css设置打印样式在style标签中使用

@media print {
	    .table {
	        overflow: hidden;
			height: 100%;
			width: 100%;
			.otherContentBoxTr{
				width: 100%;
				height: 140px;
				td{
					width: 20%;
					height: 100%;
					line-height: 140px;
				}
				.otherContentBox{
					width: 90%;
					height: 100%;
					line-height: 20px;
					text-overflow:ellipsis;
					overflow: hidden;
				}
			}
	    }
	}

预防打印滚动条的出现(打印无法分页)
js设置打印内容的所有父级元素没有宽高限制

					document.getElementsByClassName('contentBox')[0].style.height='initial';
					document.getElementsByClassName('contentBox')[0].style.width='100%';

打印后恢复页面功能(打印后点取消默认页面功能会失效)

 					window.print();
			        window.location.reload()

猜你喜欢

转载自blog.csdn.net/qq_40557812/article/details/89467574