0

JavaScriptから印刷するこのコードを見つけました。ただし、印刷するドキュメントを含むウィンドウが開きます。その文書を非表示にする方法はありますか?

var element=document.getElementById(element_id);
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');

newWin.document.open();
/* newWin.document.title = "Readings on PageLinks"; */
newWin.document.write('<html><head><title>Readings on PageLinks</title></head><body   onload="window.print()">'+element.innerHTML+'</body></html>');
newWin.document.close();

setTimeout(function(){ newWin.close(); },10);

そのドキュメントの印刷は onload() で行われるので、それなしでは印刷はできないと思います。しかし、それを隠すことはできますか?

4

2 に答える 2

1

選択した HTML 要素のみを印刷する方法で説明されているように、印刷固有のスタイルシートを使用してこれを実現できます。まったく使用しないでくださいwindow.open()。CSS クラス (必要に応じて動的に適用される) を使用して、印刷する必要がある/印刷しない要素を指定します。

于 2012-05-10T19:55:36.740 に答える
0

これをマークアップに追加します。

<iframe id="ifrOutput" style="display:none;"></iframe>

次の JavaScript 関数を追加します。

function printContainer(content, styleSheet) {
    var output = document.getElementById("ifrOutput").contentWindow;
    output.document.open();
    if (styleSheet !== undefined) {
        output.document.write('<link href="' + styleSheet + '" rel="stylesheet" type="text/css" />');
    }
    output.document.write(content);
    output.document.close();
    output.focus();
    output.print();
}

そして、次のように呼び出します。

// with stylesheet
printHtml('<div>Styled markup</div>', 'printStyles.css');

// without stylesheet
printHtml('<div>Unstyled markup</div>');
于 2012-12-13T14:12:30.787 に答える