使用Jquery打印div内容

Dee*_*ran 31 html javascript printing jquery

我想使用jQuery打印div的内容.这个问题已经在SO中提出,但我找不到正确的(有效的)答案.

这是我的HTML:

<div id='printarea'>
    <p>This is a sample text for printing purpose.</p>
    <input type='button' id='btn' value='Print'>
</div>
<p>Do not print.</p>
Run Code Online (Sandbox Code Playgroud)

在这里,我想打印div的内容printarea.

我试过这个:

$("#btn").click(function () {
    $("#printarea").print();
});
Run Code Online (Sandbox Code Playgroud)

但是单击按钮时会出现控制台错误:

未捕获的TypeError:$(...).print不是函数

但是当我尝试使用打印整个页面时

window.print();
Run Code Online (Sandbox Code Playgroud)

这是工作.但我只想打印特定div的内容.我$("#printarea").print();在许多地方看到了答案,但这不起作用.

Dee*_*ran 61

一些jQuery研究失败了,所以我转向JavaScript(感谢你的建议Anders).

它运作良好......

HTML

<div id='DivIdToPrint'>
    <p>This is a sample text for printing purpose.</p>
</div>
<p>Do not print.</p>
<input type='button' id='btn' value='Print' onclick='printDiv();'>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

function printDiv() 
{

  var divToPrint=document.getElementById('DivIdToPrint');

  var newWin=window.open('','Print-Window');

  newWin.document.open();

  newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');

  newWin.document.close();

  setTimeout(function(){newWin.close();},10);

}
Run Code Online (Sandbox Code Playgroud)

  • 你将丢失CSS/JS /字体 (7认同)

小智 33

https://github.com/jasonday/printThis

$("#myID").printThis();
Run Code Online (Sandbox Code Playgroud)

很棒的Jquery插件可以完成您的操作


And*_*ers 8

如果你想在没有额外插件的情况下这样做(比如printThis),我认为这应该有效.我们的想法是打印一个特殊的div,而其他一切都是使用CSS隐藏的.如果div是body标签的直接子项,则更容易做到,因此您必须将要打印的任何内容移动到这样的div.S首先创建一个id print-me为div的div 作为你身体标签的直接子.然后使用此代码打印div:

$("#btn").click(function () {
    //Copy the element you want to print to the print-me div.
    $("#printarea").clone().appendTo("#print-me");
    //Apply some styles to hide everything else while printing.
    $("body").addClass("printing");
    //Print the window.
    window.print();
    //Restore the styles.
    $("body").removeClass("printing");
    //Clear up the div.
    $("#print-me").empty();
});
Run Code Online (Sandbox Code Playgroud)

你需要的风格是:

@media print {
    /* Hide everything in the body when printing... */
    body.printing * { display: none; }
    /* ...except our special div. */
    body.printing #print-me { display: block; }
}

@media screen {
    /* Hide the special layer from the screen. */
    #print-me { display: none; }
}
Run Code Online (Sandbox Code Playgroud)

我们应该只@printprinting类存在时应用样式的原因是,如果用户通过选择打印页面,则应该正常打印页面File -> Print.


小智 8

我现在更新了这个功能,
您可以打印任何标签或页面的任何部分,其完整样式
必须包含 jquery.js 文件

超文本标记语言

<div id='DivIdToPrint'>
    <p>This is a sample text for printing purpose.</p>
</div>
<p>Do not print.</p>
<input type='button' id='btn' value='Print' onclick='printtag("DivIdToPrint");' >
Run Code Online (Sandbox Code Playgroud)

JavaScript

        function printtag(tagid) {
            var hashid = "#"+ tagid;
            var tagname =  $(hashid).prop("tagName").toLowerCase() ;
            var attributes = ""; 
            var attrs = document.getElementById(tagid).attributes;
              $.each(attrs,function(i,elem){
                attributes +=  " "+  elem.name+" ='"+elem.value+"' " ;
              })
            var divToPrint= $(hashid).html() ;
            var head = "<html><head>"+ $("head").html() + "</head>" ;
            var allcontent = head + "<body  onload='window.print()' >"+ "<" + tagname + attributes + ">" +  divToPrint + "</" + tagname + ">" +  "</body></html>"  ;
            var newWin=window.open('','Print-Window');
            newWin.document.open();
            newWin.document.write(allcontent);
            newWin.document.close();
           // setTimeout(function(){newWin.close();},10);
        }
Run Code Online (Sandbox Code Playgroud)

  • 这应该被标记为答案。我尝试了所有其他的东西,但没有一个起作用。他们应该因为这个答案给你一枚奖章:) (2认同)

San*_*N S 7

不使用任何插件,您可以选择此逻辑.

$("#btn").click(function () {
    //Hide all other elements other than printarea.
    $("#printarea").show();
    window.print();
});
Run Code Online (Sandbox Code Playgroud)

  • 在 Mozila firefox 上打印页面中的所有内容 (3认同)
  • 也可以在 Chrome v.72 上打印页面中的所有内容。 (3认同)

小智 7

上述解决方案都不是完美的。它们要么丢失 CSS,要么必须包含/编辑外部 CSS 文件。我找到了一个完美的解决方案,它不会丢失您的 CSS,您也不必编辑/添加外部 CSS。

HTML:

<div id='printarea'>
    <p>This is a sample text for printing purpose.</p>
    <input type='button' id='btn' value='Print' onclick='printFunc();'>
</div>
<p>Do not print.</p
Run Code Online (Sandbox Code Playgroud)

Javascript:

function printFunc() {
    var divToPrint = document.getElementById('printarea');
    var htmlToPrint = '' +
        '<style type="text/css">' +
        'table th, table td {' +
        'border:1px solid #000;' +
        'padding;0.5em;' +
        '}' +
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write("<h3 align='center'>Print Page</h3>");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

使用这个库:Print.JS

使用此库,您可以打印 HTML 和 PDF。

<form method="post" action="#" id="printJS-form">
    ...
 </form>

 <button type="button" onclick="printJS('printJS-form', 'html')">
    Print Form
 </button>
Run Code Online (Sandbox Code Playgroud)


Ama*_*rja 5

下面来自Codepen的代码为我工作了,

function printData()
{
   var divToPrint=document.getElementById("printTable");
   newWin= window.open("");
   newWin.document.write(divToPrint.outerHTML);
   newWin.print();
   newWin.close();
}

$('button').on('click',function(){
printData();
})
Run Code Online (Sandbox Code Playgroud)

这是链接codepen


Inv*_*ala 5

<div id='printarea'>
    <p>This is a sample text for printing purpose.</p> 
</div>
<input type='button' id='btn' value='Print' onlick="printDiv()">
<p>Do not print.</p>

function printDiv(){
        var printContents = document.getElementById("printarea").innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
}
Run Code Online (Sandbox Code Playgroud)

上述功能应在同一页面上加载。


小智 5

首先包含标题

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> 

<script type="text/JavaScript" src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.print/1.6.0/jQuery.print.js"></script>
Run Code Online (Sandbox Code Playgroud)

由于您使用带有选择器的打印函数,该选择器是 print.js 的一部分,因此您需要在使用之前调用它们......

Else 
    window.print()  
Run Code Online (Sandbox Code Playgroud)

会做的

$("#btn").click(function () {
    $("#printarea").print();
});
Run Code Online (Sandbox Code Playgroud)

或者

$("#btn").on('click',function () {
    $("#printarea").print();
});
Run Code Online (Sandbox Code Playgroud)