我正在尝试在打印时将css文件添加到HTML内容中。但是没有添加css文件。我没有CSS得到输出。任何人都可以帮助我......
这是我的代码
<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'mydiv', 'height=700,width=1200');
mywindow.document.write('<html><head><title></title>');
mywindow.document.write('<link rel="stylesheet" href="print.css" type="text/css" media="print"/>');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
return true;
}
</script>
</head>
<body>
<div id="mydiv">
This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante.
</div>
<div>
This will not be printed.
</div>
<div id="anotherdiv">
Nor will this.
</div>
<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />
</body>
</html>
这是我的CSS文件'print.css'
@media print {
#mydiv {
border: 10px solid !important;
color: red;
font-size:20px !important;
}
}
答案 0 :(得分:0)
找出问题所在:
解决方案请将整个div传递给popup函数
然后将div.html()传递给它尝试这样的事情或尝试其他方式
function PrintElem(elem)
{
Popup($(elem)); // send div not html only
}