2012-08-10 79 views
-3

我在我的網站上顯示縮小尺寸的圖像。我希望能夠點擊該圖像,並打開全尺寸圖像並打印出來。我在網上看過幾個例子,但都是指打印出正在顯示的圖像。我需要將全尺寸的圖像打開並打印以便清晰。在此先感謝在網站上打印圖像

+0

我已經嘗試使用javascript:image.print,但它只會打印正在顯示的圖像,這是縮略圖版本 – syllable3 2012-08-10 01:58:13

+0

最簡單的方法是做target = _blank和window.print()?不知道是否會有另一個可以將它們結合在一起的選項 – syllable3 2012-08-10 02:04:14

回答

1

可以隱藏另一分區內較大的圖像和打印圖像,而不是像

<script type="text/javascript"> 

function printDiv(divName) { 
    var printContents = document.getElementById(divName).innerHTML; 
    var originalContents = document.body.innerHTML; 

    document.body.innerHTML = printContents; 

    window.print(); 

    document.body.innerHTML = originalContents; 
} 

</script> 

<img src="images/smaller-img.jpg" /> 

<a href="#" onClick="printDiv('largeimg')">Print</a> 

<div id="largeimg" style="display:none;"> 
<img src="images/larger-img.jpg"> 
</div> 
0

HTML代碼

<iframe id="ifrmPrint" src="#" style="width:0pt; height:0pt; border: none;"></iframe> 
<td colspan="2" align="center"><input onclick="print_imgs();" class="button" type="button" title="Print all the images which are displayed in slide show" value="Print All"></td> 

JAVA SCRIPT

function img_find() { 
      var imgs = document.getElementsByTagName("img"); 
      var imgSrcs = []; 

      for (var i = 0; i < imgs.length; i++) { 
       imgSrcs.push(imgs[i].src); 
      } 

      return imgSrcs; 
     } 

     function print_imgs(){ 
      var imgSrcs = img_find(); 
      try{ 
       var print_frame = document.getElementById('ifrmPrint'); 
       var print_doc = (print_frame.contentWindow || print_frame.contentDocument); 
       if (print_doc.document) print_doc = print_doc.document; 
       // Write printable content 
       print_doc.write("<html><head>"); 
       print_doc.write("<title>printing all images</title>"); 
       print_doc.write('</head><body onload="this.focus(); this.print();">'); 
       for(var i=0; i< imgSrcs.length; i++){ 
        print_doc.write("<img src='" + imgSrcs[i] + "' /><br>"); 
       } 
       print_doc.write("</body></html>"); 
       print_doc.close(); 
     } 
     catch(e){ 
       alert("printint in error state"); 
       self.print(); 
     } 
     } 
+4

您能否解釋_how_這回答了這個問題? – Ben 2014-09-22 13:07:23