簡體   English   中英

如何使用JavaScript在沒有PrintPreview的情況下打印div

[英]How to print a div without PrintPreview using javascript

我想在不顯示預覽窗口的情況下打印特定的div。 請幫忙

function PrintInvoice() {
        $(document).ready(function () {
            var toPrint = document.getElementById('div_print');
            var popupWin = window.open('', '_blank', 'width=1000,location=no,scrollbars=1,tollbar=0,top=0,left=200px,menubar=no,resizable=no');
            popupWin.document.open();
            popupWin.document.write('<html><body onload="window.print()">')
            popupWin.document.write(toPrint.innerHTML);
            popupWin.document.write('</html>');
            popupWin.document.close();
            popupWin.focus();
            popupWin.print();
            popupWin.close();
        });
    }

上面的代碼首先打開一個新窗口,然后顯示打印對話框。 我不想打開一個新窗口

試試這個

<head>
<style type="text/css">

#printable { display: none; }

@media print
{
    #non-printable { display: none; }
    #printable { display: block; }
}
</style>
</head>
<body>
<div id="non-printable">
    Your normal page contents
</div>

<div id="printable">
    Printer version
</div>

檢查一下。

<form>
  <input type="button" value="Print Page" onClick="window.print()">
</form>

<script language="VBScript">
  // THIS VB SCRIP REMOVES THE PRINT DIALOG BOX AND PRINTS TO YOUR DEFAULT PRINTER
  Sub window_onunload()
  On Error Resume Next
  Set WB = nothing
  On Error Goto 0
  End Sub

  Sub Print()
  OLECMDID_PRINT = 6
  OLECMDEXECOPT_DONTPROMPTUSER = 2
  OLECMDEXECOPT_PROMPTUSER = 1


  On Error Resume Next

  If DA Then
  call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)

  Else
  call WB.IOleCommandTarget.Exec(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","","")

  End If

  If Err.Number <> 0 Then
   If DA Then 
    Alert("Nothing Printed :" & err.number & " : " & err.description)
  Else
  HandleError()
 End if
 End If
 On Error Goto 0
 End Sub

If DA Then
  wbvers="8856F961-340A-11D0-A96B-00C04FD705A2"
Else
   wbvers="EAB22AC3-30C1-11CF-A7EB-0000C05BAE0B"
End If

document.write "<object ID=""WB"" WIDTH=0 HEIGHT=0 CLASSID=""CLSID:"
document.write wbvers & """> </object>"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM