dounangshen6553 2009-10-02 11:58
浏览 90
已采纳

如何创建触发文件打印的链接?

I have some small images and each has a "print larger image" link.

For example:

<img src="thumb.jpg">
<a href="#">Print Larger Image</a>

When the user clicks the "print larger image" link, my goal is to trigger the printing of "big.jpg".

How can this be done?

EDIT: ANTHONY HAD THE RIGHT SOLUTION FOR ME. I MODIFIED HIS CODE A BIT TO GET IT WORKING. HERE'S THE FULL, WORKING CODE

<style>
@media screen {
.printimage {
     display: none;
}
}

@media print {
body * {
    display: none;
}
.printimage {
     display: block;
}
}

</style>

<a id="bigimage" href="#">Print Big Image</a>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
 $(document).ready(function(){

     $("#bigimage").click(function() {
          $("body").append("<img src='http://sstatic.net/so/img/logo.png' class='printimage' >");
          window.print();
     });

});

</script>
  • 写回答

5条回答 默认 最新

  • douejuan9162 2009-10-02 12:27
    关注

    This would be really weird and awesome, but one thing you could do is:

    Have the print large image link trigger a javascript function that inserts the larger image into the DOM with a class of "printable_image".

    Then have your CSS rules set up with an @screen and @print rules that hide the image in the browser but hide everything but the image for the printout.

    Then call the already mentioned window.print() function. The only catch is that users wouldn't see anything if they just wanted to print the screen, so you'd have to add the @print rule with the js to avoid this.

    The HTML:

    <img src="thumb.jpg">
    <a href="#" id="bigimage">Print Larger Image</a>
    

    The JS (in jquery):

    $function() {
         $("#bigimage").click(function() {
              $(body).amend("<img src='bigimage.png' class="printimage" />");
         });
    });
    

    The CSS:

    @media screen {
    .printimage {
         display: none;
    }
    }
    
    @media print {
    body * {
        display: none;
    }
    .printimage {
         display: block;
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败