如何设置用于横向打印的 javascript 功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24465125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:50:54  来源:igfitidea点击:

How to set the javascript function for print into landscape?

javascriptjqueryiosprinting

提问by MoonPrincess

I was trying to set the print orientation into landscape through javascript. I'm only just using the window.print() function for the page's content to be printed. here's the code:

我试图通过 javascript 将打印方向设置为横向。我只是使用 window.print() 函数来打印页面的内容。这是代码:

<a href="" onClick="window.print();return false">
<img src="images/print.png"></a>

With this I was able to print a sample page. But only it was on portrait format. The client requires it to be in landscape. so I tried on many codes I found. here's the one am sure your most familiar with.

有了这个,我就可以打印示例页面了。但只有它是纵向格式。客户要求它处于横向。所以我尝试了很多我找到的代码。这是您最熟悉的一个。

<script type="text/javascript">
$("#printLandscape").on('click',function () {
$('head').append('<link href="printLandscape.css" title="printLandscape" rel="stylesheet" />');
});
</script>

since it wasn't a button that am using to call the print function but an image type with href, I tried to change the previous one with:

由于它不是用于调用打印功能的按钮,而是带有 href 的图像类型,因此我尝试将前一个更改为:

<a href="" onClick="window.print();return false" id="printLandscape">
<img src="images/print.png"></a>

See the id="printLandscape", I tried that on to be able to call the javascript function declared above and make the page print into my desired format which is landscape. But when I tried to print, to see if it was working. The printed page still has the default/formal or portrait type.

参见 id="printLandscape",我尝试了它,以便能够调用上面声明的 javascript 函数并使页面打印成我想要的横向格式。但是当我尝试打印时,看看它是否有效。打印的页面仍然具有默认/正式或纵向类型。

Is there something am missing? Or did I not declare the javascript properly especially the id on href for it not to work fine?

有什么遗漏吗?或者我没有正确声明javascript,尤其是href上的id,因为它不能正常工作?

S.O.S

求救

回答by Albert-Jan Stevens

I think you should make the page property landscape in CSS:

我认为您应该在 CSS 中使页面属性横向:

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

The support is left to the browser and it appears not to be supported in all browsers.

支持留给浏览器,似乎并非所有浏览器都支持。