gpt4 book ai didi

javascript - 更改 Window.print() 纸张方向

转载 作者:IT老高 更新时间:2023-10-28 22:59:28 26 4
gpt4 key购买 nike

我想更改窗口打印的纸张模式(方向)。我想以编程方式更改它,但我找不到任何东西。

window.print()

但我不知道,我该怎么做。

@media print{@page {size: landscape}}

我不需要它。

    function printWindow()
{
window.print({
/*
some code here?
*/
});
}

最佳答案

您需要为文档注入(inject)样式。

var css = '@page { size: landscape; }',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');

style.type = 'text/css';
style.media = 'print';

if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}

head.appendChild(style);

window.print();

//don't forget to find and remove style if you don't want all you documents stay in landscape

https://jsfiddle.net/vc4jjhpn/

关于javascript - 更改 Window.print() 纸张方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025840/

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