gpt4 book ai didi

css - 无法将样式表/脚本插入 window.open

转载 作者:技术小花猫 更新时间:2023-10-29 12:07:30 26 4
gpt4 key购买 nike

我已经为这个问题奋斗了很长一段时间,并且一直(仍然)无法打印我的 div 及其样式。

目前,我的脚本是:

$('#printMeButton').click(function () {
//alert("a");
var data = document.getElementById('thisPrintableTable').outerHTML;

var mywindow = window.open('', data);
mywindow.document.write('<html><head><title>Print Me!!!</title>');
// mywindow.document.write('<link rel="stylesheet" type="text/css" href="Site.css" media="screen">');
mywindow.document.write('</head><body>');
mywindow.document.write(data);
mywindow.document.write('</body></html>');

mywindow.document.close();
mywindow.focus();
mywindow.print();
mywindow.close();
return true;

});

嵌套在 $(document).ready 中功能。

当我包含所需的样式表(当前已注释掉)时,打印预览中没有显示任何内容。

我还有一些脚本可以影响表格的外观,因此,我相信这可能是将这些包含在弹出窗口中的关键。

我怎样才能将它包含到新的弹出窗口中?

有人可以建议一种按原样打印的方法吗?

编辑历史

  • 删除了 </head><body> 末尾的空格
  • 已更改 var dataouterHTML而不是 innerHTML
  • 为了更好地理解问题而改变问题/细节

最佳答案

尝试使用带有 css 链接的 window.open 打开本地 html 文件。并使用js设置要打印的内容html到本地html文件。

这是要打印的页面:-

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="test.css" rel="stylesheet" type="text/css" />
<script src="jquery.js"></script>
</head>
<body>
<div id="print">
<div class="red">TODO write content</div>
</div>
<button id="print_btn">Print</button>
<script>
$('#print_btn').click(function(){
var newWindow = window.open('print.html','_blank');
$(newWindow).load(function(){
$(newWindow.document).find('body').html($('#print').html());
});
})
</script>
</body>
</html>

css文件test.css链接在这里,我在window.open的时候打开print.html,test.css也链接在print.html中

现在,在 print.html 中我会写:-

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body>

</body>
</html>

关于css - 无法将样式表/脚本插入 window.open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27268651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com