最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - printing iframe content with jquery - Stack Overflow

matteradmin6PV0评论

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 changes based on the link he clicks - iframe content prints

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 changes based on the link he clicks - iframe content prints

Share Improve this question edited Apr 11, 2012 at 8:56 seferov 4,1613 gold badges41 silver badges76 bronze badges asked Aug 2, 2011 at 0:30 scarhandscarhand 4,33724 gold badges67 silver badges93 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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 e 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");
}
Post a comment

comment list (0)

  1. No comments so far