1
我有用于打印div内容的此javascript代码。将css写入javascript代码does not work
function PrintElem()
{
var mywindow = window.open('', 'PRINT', 'height=600,width=800');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('<style type="text/css">' + @media print {
.no_print{ display:none;}
.fav_thumb{width:50%; height:50%; float:left}
} + '</style>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h2>' + document.title + '</h2>');
mywindow.document.write(document.getElementById("favorite_items").innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;
}
我该怎么写一些打印CSS的新窗口,JavaScript打开?
首先,您要添加的CSS(以'@ media'开头)需要封装在引号中。 –
如果CSS引用正确,可以正常工作https://codepen.io/pjabbott/pen/XVMrOe –