2012-03-19 134 views
1

我希望通過傳遞custome html代碼來打印文檔。通過javascript打印html/jquery

非工作代碼,我已經寫了:

function Clickheretoprint() 
{ 
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
     disp_setting+="scrollbars=yes,width=780, height=780, left=100, top=25"; 
    var content_vlue = document.getElementById("result_tbl").innerHTML; 

    var docprint=window.open("","",disp_setting); 
    docprint.document.open(); 
    docprint.document.write('<html><head><title>Ashley Mattresses</title>'); 
    docprint.document.write('</head><body onLoad="self.print()"><center>');   
    docprint.document.write('<TABLE width="100%" cellpadding=10 align=center valign="top"><TR valign="top"><TD width = "33%" valign="top">col1</TD><TD width = "33%" valign="top">col2</TD><TD width = "33%" valign="top">col3</TD></TR></TABLE>');  
    docprint.document.write('</center></body></html>'); 
    docprint.document.close(); 
    docprint.focus(); 
    docprint.close(); 
} 

這是我叫一個錨標記的HREF的方法,但沒有得到所做的工作:

<a href="javascript:Clickheretoprint()"> 

我作爲JavaScript/jQuery編碼的新寵兒,請幫助我實現這一目標(通過糾正這一問題或提出一種方法),作爲其迫切要求。

在此先感謝。

+2

OH NO迫切?什麼不起作用?控制檯中是否有錯誤?你知道可以在沒有新窗口的情況下只在頁面上打印某些東西。看看CSS打印介質。 – epascarello 2012-03-19 12:22:12

+0

@epascarello ya我重新檢查了一點修改,發現在Firefox中工作,而不是在谷歌瀏覽器。早些時候我正在測試Chrome本身,所以無法打印。但也需要使用Chrome瀏覽器,請問您可以提出一些建議嗎? 此外,感謝您對CSS打印介質的建議,它非常好,並且肯定會要求此功能。 – HarsH1610 2012-03-19 13:33:22

+0

答案#2是正確的,但您必須添加:var disp_setting =「menubar = no; status = no; toolbar = no;」;或您選擇的設置 – 2013-02-12 21:20:55

回答

7

您正處於正確的軌道上。假設result_tbl是你想打印原樣,先用<div>標籤例如爲:

<div> 
    <table id="result_tbl"> 
     ..... 
    </table> 
</div> 

包裹元件然後讓這樣的代碼元素的ID:

var docprint=window.open("about:blank", "_blank", disp_setting); 
var oTable = document.getElementById("result_tbl"); 
docprint.document.open(); 
docprint.document.write('<html><head><title>Ashley Mattresses</title>'); 
docprint.document.write('</head><body><center>'); 
docprint.document.write(oTable.parentNode.innerHTML); 
docprint.document.write('</center></body></html>'); 
docprint.document.close(); 
docprint.print(); 
docprint.close(); 

Live test case

Chrome的18歲以下的罰款的工作對我來說:

print is working

+0

感謝您的努力,檢查了現場測試案例,您已經提供了上面的,它的工作原理和通過mozilla-firefox打印很好,但無法用鉻做同樣的事情,請你重新檢查,並建議我可能的出路? – HarsH1610 2012-03-19 12:59:24

+0

@ HarsH1610 Chrome 18在這裏,點擊按鈕打開瀏覽器的打印頁面就好了。 – 2012-03-19 14:30:52

+0

你能否檢查一下:在chrome的打印頁面中,打印按鈕被禁用,並且沒有任何頁面的打印預覽;你的結局是否一樣?請建議! – HarsH1610 2012-03-19 15:03:55

1

結帳:http://jsfiddle.net/5Wfqr/3/

對我來說只是麻煩的來源是:你的HTML中的 「result_tbl」:

var content_vlue = document.getElementById("result_tbl").innerHTML; 

你有ID的元素?

+0

是的,我確實擁有它,只是忘了從這個示例代碼中刪除它的引用,但它不起作用,雖然我刪除了它。 – HarsH1610 2012-03-19 12:53:15

+0

感謝您使用firefox而不是使用google-chrome瀏覽器的努力,您能否提出建議? – HarsH1610 2012-03-19 13:31:58

0

此代碼工作正常,我

<body> 
     <script type="text/javascript"> 
      function printPage() { 

       var docprint = window.open("about:blank", "_blank"`enter code here`, "channelmode");  
       var oTable = document.getElementById("tbl"); 
       docprint.document.open(); 
       docprint.document.write('<html><head><title>your title</title>'); 
       docprint.document.write('</head><body><center>'); 
       docprint.document.write(oTable.innerHTML); 
       docprint.document.write('</center></body></html>'); 
       docprint.document.close(); 
       docprint.print(); 
       docprint.close(); 
      } 
     </script> 
     <table id="tbl"> 
      <tr> 
       <td> content1 </td> 
       <td> content2 </td> 
      </tr> 
     </table> 
     <input type="button" value ="print" id="printButton" onclick="javascript:printPage()"/> 
    </body>