Javascript 在chrome中打印div,css不起作用
我正在使用window.print();对于网页上的打印特定部分,它在firefox浏览器上工作,但在Chrome浏览器中工作不正常,显示空白打印预览Javascript 在chrome中打印div,css不起作用,javascript,jquery,html,css,google-chrome,Javascript,Jquery,Html,Css,Google Chrome,我正在使用window.print();对于网页上的打印特定部分,它在firefox浏览器上工作,但在Chrome浏览器中工作不正常,显示空白打印预览 <script type="text/javascript"> function printDiv() { var divToPrint = document.getElementById('table'); var htmlToPrint = '' + '<link href="../
<script type="text/javascript">
function printDiv() {
var divToPrint = document.getElementById('table');
var htmlToPrint = '' +
'<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />'+
'<link href="../dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />';
$(document).ready(function() {
$('#customers').dataTable();
} );
htmlToPrint += divToPrint.outerHTML;
newWin = window.open("");
newWin.document.write(htmlToPrint);
newWin.print();
newWin.close();
}
</script>
函数printDiv(){
var divToPrint=document.getElementById('table');
var htmlToPrint=''+
''+
'';
$(文档).ready(函数(){
$(“#客户”).dataTable();
} );
htmlToPrint+=divToPrint.outerHTML;
newWin=window.open(“”);
newWin.document.write(htmlToPrint);
newWin.print();
newWin.close();
}
上面的代码在chrome中也可以正常工作,但从过去15天开始,它在chrome中不能正常工作,但在firefox中可以正常工作
<script type="text/javascript">
$(function () {
$("#btnPrint").click(function () {
var contents = $("#table").html();
var frame1 = $('<iframe />');
frame1[0].name = "frame1";
frame1.css({ "position": "absolute", "top": "-1000000px" });
$("body").append(frame1);
var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
frameDoc.document.open();
//Create a new HTML document.
frameDoc.document.write('<html><head><title>DIV Contents</title>');
frameDoc.document.write('</head><body>');
//Append the external CSS file.
frameDoc.document.write('<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />');
frameDoc.document.write('<link href="../dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />');
//Append the DIV contents.
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
frame1.remove();
}, 500);
});
});
</script>
$(函数(){
$(“\btnPrint”)。单击(函数(){
var contents=$(“#表”).html();
变量frame1=$('');
frame1[0]。name=“frame1”;
frame1.css({“位置”:“绝对”,“顶部”:“-1000000px”});
$(“正文”)。追加(第1帧);
var frameDoc=frame1[0]。contentWindow?frame1[0]。contentWindow:frame1[0]。contentDocument.document?frame1[0]。contentDocument.document:frame1[0]。contentDocument;
frameDoc.document.open();
//创建一个新的HTML文档。
frameDoc.document.write('DIV Contents');
frameDoc.document.write(“”);
//附加外部CSS文件。
frameDoc.document.write(“”);
frameDoc.document.write(“”);
//附加DIV内容。
frameDoc.document.write(内容);
frameDoc.document.write(“”);
frameDoc.document.close();
setTimeout(函数(){
window.frames[“frame1”].focus();
window.frames[“frame1”].print();
框架1.移除();
}, 500);
});
});
也许这是chrome 50版本的问题,我对新版本也有问题。你的版本是什么?我的chrome版本50.0.2661.87 m