我有一个PHP页面,我需要放置一个print
按钮。为此,我在下面添加了Javascript:
<script type="text/javascript">
function PrintContent() {
var DocumentContainer = document.getElementById('prnt');
var WindowObject = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
}
</script>
现在用一些css打印div prnt
,我添加了一个css
<link rel="stylesheet" type="text/css" href="css/print.css" media="print">
我的print.css
是:
.preorder_list
{
overflow:hidden;
}
.preorder_list h2
{
font-family:Verdana, Arial, Helvetica, sans-serif;
color:#0e337f;
font-size:17px;
margin-top:40px;
}
.preorder_list table
{
width:750px;
background:#d5d5d5;
padding:8px 4px 8px 4px;
border-radius: 4px;
-moz-border-radius:4px;
-webkit-border-radius: 4px;
border:1px solid #dbdbe2;
}
.preorder_list th
{
padding:5px;
}
.preorder_list td
{
text-align:center;
font-family:"Trebuchet MS";
font-size:15px;
padding:5px;
}
.preorder_list input[type="text"]
{
width:50px;
border:1px solid #b10c04;
text-align:center;
}
.preorder_list input[type="text"]:focus
{
color:#1226a1;
font-weight:bold;
}
.preorder_list.mw
{
padding:10px 0 10px 20px;
border:1px solid #e4e3df;
}
.preorder_list.mw:hover
{
background:#fbfbfa;
}
.preorder_list.sw
{
margin-top:20px;
padding:10px 0 10px 20px;
border:1px solid #e4e3df;
}
.preorder_list h1
{
color:#1b2d58;
font-size:18px;
font-family: "Franklin Gothic Medium", "Franklin Gothic", "ITC Franklin Gothic", Arial, sans-serif;
}
.preorder_list.sw:hover
{
background:#fbfbfa;
}
preorder_list
是所有数据所在的类,上面的代码与我以前显示的css
相同。但是,当我点击Pirnt
时,我看不到所需的外观,请查看图片。
编辑:
修改后的javascript代码:
var DocumentContainer = document.getElementById('prnt');
var WindowObject = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
WindowObject.document.writeln('<!DOCTYPE html>');
WindowObject.document.writeln('<html><head><title></title>');
WindowObject.document.writeln('<link rel="stylesheet" type="text/css" href="css/print.css" media="print">');
WindowObject.document.writeln('</head><body>');
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.writeln('</body></html>');
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
答案 0 :(得分:3)
如果您要使用document.write()
创建文档,则必须创建整个文档。试试这个:
WindowObject.document.writeln('<!DOCTYPE html>');
WindowObject.document.writeln('<html><head><title></title>');
WindowObject.document.writeln('<link rel="stylesheet" type="text/css" href="css/print.css">');
WindowObject.document.writeln('</head><body>');
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.writeln('</body></html>');
请注意,media="print"
仅对打印本身有影响,页面未在屏幕上设置样式。您可以省略该属性,或使用media="all"
代替。