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

3

我的页面中有一个 <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呢?
1个回答

7

试试这个:

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接