如何打印弹出窗口内容

时间:2014-03-24 09:57:03

标签: javascript jquery

我尝试了以下内容:

<button onclick="window.print()" class="uk-button uk-float-left"><?php echo JText::_('COM_CSTUDOMUS_IMPRIMIR'); ?></button>

另外:

self.print()

window.focus()的; window.print()

当我点击打印时,它会显示主窗口和要打印的页面中的弹出窗口。我只需要弹出窗口的内容。

2 个答案:

答案 0 :(得分:4)

这是打印弹出窗口的一个示例:

<div class="contentSection">
    <div class="contentToPrint">
        <!-- content to be printed here -->
    </div>
</div>

<div class="contentSection">
    <a href="#" id="printOut">Print This</a>
</div>

<div class="contentSection termsToPrint">
    <h4>Terms & conditions</h4>
    <p>Management reserves the right to withdraw, amend or suspend this print job in the event of any unforeseen circumstances outside its reasonable control, with no liability to any third party.</p>
</div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

<script type="text/javascript">
    $(function(){
        $('#printOut').click(function(e){
            e.preventDefault();
            var w = window.open();
            var printOne = $('.contentToPrint').html();
            var printTwo = $('.termsToPrint').html();
            w.document.write('<html><head><title>Copy Printed</title></head><body><h1>Copy Printed</h1><hr />' + printOne + '<hr />' + printTwo) + '</body></html>';
            w.window.print();
            w.document.close();
            return false;
        });
    });
</script>

答案 1 :(得分:1)

//popup page
   <html>
        <head>
        <title>Your popup</title>
        </head>
        <body>

        <h1>Pop</h1>
        <p>Print me</p>
        <a href="print.html" onclick="window.print();return false;">
            <img src="images/printer.png" height="32px" width="32px">
        </a>

        </body>
      </html>

//Main page
    <html>
        <head>
        <title>main</title>
        </head>
        <body>

        <h1>Pop & print</h1>
        <button onclick="pop();">Pop</button>
        <script type="text/javascript">
          var POP;
          function pop() {
            POP = window.open('popup.html', 'thePopup', 'width=350,height=350');
          }
        </script>

        </body>
      </html>