1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>print</title>
</head>
<body>
<style>
.border {
border: 1px solid red;
}
.box1 {
width: 300px;
height: 400px;
background: red;
}
.box2 {
width: 300px;
height: 400px;
border: 1px solid blue;
}
@media print {
.box2 {
display: none;
}
.box1 {
/* 无效果 */
background-color: aqua;
border-color: blueviolet;
}
.print-button {
display: none;
}
}
</style>
<div class="box1 border">
box1 打印显示
</div>
<div class="box2">
box2 打印隐藏
</div>
<button onclick="printDocument()" class="print-button">打印整个文档</button>
<script>
function printDocument () {
window.print()
}
</script>
</body>
</html>
2.效果
3.如何调试打印样式
其中背景色样式在实际进行打印输出时不起作用,并没有对应的背景色,虽然预览的时候存在背景色