首页 文章

如何在单击按钮时打印HTML内容,而不是页面? [重复]

提问于
浏览
86

这个问题在这里已有答案:

当用户点击按钮时,我想打印一些HTML内容 . 一旦用户点击该按钮,浏览器的打印对话框将打开,但不会打印该网页 . 相反,它将打印一些未显示在页面上的其他HTML内容 .

在提出这个问题时,我想到的解决方案很少 . 但我不确定这些是好主意还是可以做得更好 . 其中一个解决方案是:我可以将这个HTML内容保存在div中并使其 display: 打印,但 display: none 来屏幕 . 网页上的所有其他元素可以打印为 display: none ,屏幕为 display: . 然后打电话给打印 .

有什么好主意吗?

6 回答

  • 128

    我遇到了另一个优雅的解决方案:

    将您的可打印部分放在一个id为div的div中:

    <div id="printableArea">
          <h1>Print me</h1>
    </div>
    
    <input type="button" onclick="printDiv('printableArea')" value="print a div!" />
    

    现在让我们创建一个非常简单的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;
    }
    

    消息来源:SO Answer

  • 54

    这是一个纯粹的CSS版本

    .example-print {
        display: none;
    }
    @media print {
       .example-screen {
           display: none;
        }
        .example-print {
           display: block;
        }
    }
    
    <div class="example-screen">You only see me in the browser</div>
    
    <div class="example-print">You only see me in the print</div>
    
  • 7

    根据this SO link,您可以打印特定的div

    w=window.open();
    w.document.write(document.getElementsByClassName('report_left_inner')[0].innerH‌​TML);
    w.print();
    w.close();
    
  • 10

    I Want See This

    示例http://jsfiddle.net/35vAN/

    <html>
    <head>
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
    <script type="text/javascript">
    
        function PrintElem(elem)
        {
            Popup($(elem).html());
        }
    
        function Popup(data) 
        {
            var mywindow = window.open('', 'my div', 'height=400,width=600');
            mywindow.document.write('<html><head><title>my div</title>');
            /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
            mywindow.document.write('</head><body >');
            mywindow.document.write(data);
            mywindow.document.write('</body></html>');
    
            mywindow.print();
            mywindow.close();
    
            return true;
        }
    
    </script>
    </head>
    <body>
    
    <div id="mydiv">
        This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
    </div>
    
    <div>
        This will not be printed.
    </div>
    
    <div id="anotherdiv">
        Nor will this.
    </div>
    
    <input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />
    
    </body>
    
    </html>
    
  • 73

    以下代码可能会帮助您:

    <html>
    <head>
    <script>
    function printPage(id)
    {
       var html="<html>";
       html+= document.getElementById(id).innerHTML;
    
       html+="</html>";
    
       var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status  =0');
       printWin.document.write(html);
       printWin.document.close();
       printWin.focus();
       printWin.print();
       printWin.close();
    }
    </script>
    </head>
    <body>
    <div id="block1">
    <table border="1" >
    </tr>
    <th colspan="3">Block 1</th>
    </tr>
    <tr>
    <th>1</th><th>XYZ</th><th>athock</th>
    </tr>
    </table>
    
    </div>
    
    <div id="block2">
        This is Block 2 content
    </div>
    
    <input type="button" value="Print Block 1" onclick="printPage('block1');"></input>
    <input type="button" value="Print Block 2" onclick="printPage('block2');"></input>
    </body>
    </html>
    
  • 2

    如果你添加和删除innerHTML,所有javascript,css和更多都将被加载两次,并且事件将触发两次(发生在我身上),更好的隐藏内容,使用jQuery和css如下:

    function printDiv(selector) {
        $('body .site-container').css({display:'none'});
        var content = $(selector).clone();
        $('body .site-container').before(content);
        window.print();
        $(selector).first().remove();
        $('body .site-container').css({display:''});
    }
    

    div“site-container”包含所有站点,因此您可以调用以下函数:

    printDiv('.someDiv');
    

相关问题