<script type="text/javascript">
function PrintElem(elem) {
Popup($(elem).html());
}
function Popup(data) {
var mywindow = window.open('', 'mydiv', 'height=550,width=750');
mywindow.document.write('<html><head><title></title>');
mywindow.document.write('<link rel="stylesheet" href="~/Content/Site.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>
这是我的 Javascript,它可以打印 DIV,并且运行良好。但是我的 CSS 没有加载,我知道是因为我测试了粉红色,但它仍然没有显示出来。请帮忙。这是我的 CSS
@media print {
.mydiv {
background-color: white;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
color:pink;
}
}
最佳答案
问题很可能是因为您的 CSS 试图引用 WINDOW 的名称,而不是 WINDOW 中的任何对象。因为我们不知道作为数据
发送的是什么,试试这个:
改变
mywindow.document.write(data);
到
mywindow.document.write("<div class='mydiv'>" + data + "</div>");
关于javascript - 使用 CSS 打印 DIV 内容,不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36941324/