方法
printdiv(printpage) { var css = "<style> " + " .zbtable { " + " border-collapse: collapse; " + " text-align: center; " + " 100%; " + " } " + " " + " .zbtable >>> th { " + " padding: 10px 0px; " + " border: 1px solid #3F3F3F; " + " 15%; " + " } " + " " + " .zbtable >>> td { " + " padding: 10px 0px; " + " border: 1px solid #3F3F3F; " + " 15%; " + " } " + " .rowStyle { " + " font-size: 16px; " + " margin-top: 20px; " + " } " + " .diBtn { " + " margin: auto; " + " margin-right: 26px; " + " margin-bottom: 10px; " + " } " + " .el-col.el-col-8 { " + " float: left; " + " 33.3333%; " + " box-sizing: border-box; " + " } " + " td { " + " padding: 10px 0px; " + " border: 1px solid #3F3F3F; " + " 15%; " + " } " + " th { " + " padding: 10px 0px; " + " border: 1px solid #3F3F3F; " + " 15%; " + " }" + "</style>"; var headstr = "<html><head><title></title>" + css + "</head><body>"; var footstr = "</body>"; var newstr = document.all.item(printpage).innerHTML; //var oldstr = document.body.innerHTML; var OpenWindow = window.open("", "newwin", "toolbar=no ,scrollbars=" + scroll + ",menubar=no"); // //写成一行 OpenWindow.document.write(headstr + newstr + footstr); OpenWindow.document.close(); OpenWindow.window.print(); OpenWindow.window.close(); }
使用
<el-button type="primary" size="small" @click="printdiv('noticeDiv')">打印</el-button>
点击按钮即可打印 id为noticeDiv的div