gpt4 book ai didi

javascript document.write css 不适用

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:25 24 4
gpt4 key购买 nike

HTML:

<div id="dvContainer">
This content needs to be printed.
</div>
<button>Print</button>

JavaScript:

 $("button").on("click", function(){
var divContents = $("#dvContainer").html();
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV Contents</title>');
printWindow.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/product.css\" />");
//printWindow.document.write("<style>#dvContainer{color: red;}</style>");

printWindow.document.write('</head><body>');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
});

我需要在窗口出现时将我的 div 更改为红色,但什么也没发生。

我尝试使用样式代码,但我的元素仍然是黑色。我也试过媒体打印但没有。

有什么建议吗?我做错了什么?
我正在最新版本的 Chrome 和 jQuery 1.9.1 中对此进行测试。

最佳答案

在我们的对话之后,您要做的是正确设置打印样式。将此添加到 products.css

@media print {
body {
background-color: red;
}
}

关于javascript document.write css 不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35687952/

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