2017-12-27 13 views
1

私はdivコンテンツを印刷するためにこのjavascriptコードを持っています。JavaScriptをコードに書き込まないでください

function PrintElem() 
{ 
    var mywindow = window.open('', 'PRINT', 'height=600,width=800'); 

    mywindow.document.write('<html><head><title>' + document.title + '</title>'); 

    mywindow.document.write('<style type="text/css">' + @media print { 
    .no_print{ display:none;} 
    .fav_thumb{width:50%; height:50%; float:left} 
} + '</style>'); 

    mywindow.document.write('</head><body >'); 
    mywindow.document.write('<h2>' + document.title + '</h2>'); 
    mywindow.document.write(document.getElementById("favorite_items").innerHTML); 
    mywindow.document.write('</body></html>'); 

    mywindow.document.close(); // necessary for IE >= 10 
    mywindow.focus(); // necessary for IE >= 10*/ 

    mywindow.print(); 
    mywindow.close(); 

    return true; 
} 

どうすれば、新しいウィンドウに印刷用のCSSを書くことができますか?JavaScriptが開きますか?

+4

まず、追加しようとしているCSS( '@ media'で始まる)を引用符で囲む必要があります。 –

+1

CSSが正しく引用されていれば正常に動作しますhttps://codepen.io/pjabbott/pen/XVMrOe –

答えて

0

オプション#1 - Inline Stylesがあなたの目標に該当する可能性があります。

インラインスタイルを使用する要素のこの段落のHTML例は、以前のリンクに表示されていました。

<p style="color:blue;font-size:46px;"> 
    I'm a big blue, <strong>strong</strong> paragraph 
</p> 

オプション#2 - 内部様式定義

オプション#3 - 外部スタイルファイルは

Styling HTML with CSSリンクは、すべての3つの(3)のオプションをカバーしています。

関連する問題