开发者

printing iframe content with jquery

开发者 https://www.devze.com 2023-03-25 09:39 出处:网络
i have an iframe: <iframe name=\"printarea\" id=\"printarea\" src=\"print.php\" style=\"display: none;\"></iframe>

i have an iframe:

<iframe name="printarea" id="printarea" src="print.php" style="display: none;"></iframe>

i need this iframe to have the style "display: none;" so it is invisible

now when someone clicks a link generated via php:

echo '<a href="#" onclick="print_receipt('.$row['id'].'); return false;"><img src="images/icon-print.png" alt="" /></a>';

i want it to simply change the iframe SRC and print what is in the iframe, however my code is not working at all:

function print_receipt (id)
{
    $('#printarea').attr('src', 'print.php?id='+id);
    $('#printarea').focus();
    $('#printarea').print();
}

so this is what i want: - user clicks on link - iframe content 开发者_JAVA技巧changes based on the link he clicks - iframe content prints


2 issues:

  1. you'll maybe need to wait until the page is loaded
  2. print() is a method of window-objects

   $('#printarea')
    .load(function(){this.contentWindow.print();$(this).unbind('load');})
     .attr('src', 'print.php?id='+id);


  1. What is $('#printarea').focus(); supposed to achieve? As far as I know .focus() works with form elements and links...
  2. What is $('#printarea').print();? Haven't come across that function in jQuery. If you're using a plugin it would help to mention which one.

So perhaps you'll have more luck with:

function print_receipt (id)
{
    $('#printarea')
       .attr('src', 'print.php?id='+id)
       .css("display","block");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消