在IE/FireFox/Chrome中横向打印DIV

Print a DIV in landscape in IE/FireFox/Chrome

本文关键字:横向 打印 DIV Chrome IE FireFox      更新时间:2023-09-26

我在我的页面中有一个<div>,并希望在IE/Firefox/Chrome中通过景观样式打印它。我用下面的代码打印div:

function print() {
    var frame = $doc.getElementsByClassName('mydivclass').item(0);
    var data = frame.innerHTML;
    var win = window.open('', '', 'height=500,width=900');
    win.document.write('<html><head><title></title>');
    win.document.write('</head><body >');
    win.document.write(data);
    win.document.write('</body></html>');
    win.print();
    win.close();
    return true;
}

我发现了这个:

@page {
  size: landscape;
}

,但它只适用于整个页面。有没有办法横向打印div ?

try this:

function print() {
    var frame = document.getElementsByClassName('mydivclass').item(0);
    var data = frame.innerHTML;
    var win = window.open('', '', 'height=500,width=900');
    win.document.write('<style>@page{size:landscape;}</style><html><head><title></title>');
    win.document.write('</head><body >');
    win.document.write(data);
    win.document.write('</body></html>');
    win.print();
    win.close();
    return true;
}