[英]Print a div content using 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>
這里我想打印 div printarea
的內容。
我試過這個:
$("#btn").click(function () {
$("#printarea").print();
});
但是單擊按鈕時會出現控制台錯誤:
未捕獲的類型錯誤:$(...).print 不是 function
但是當我嘗試使用打印整個頁面時
window.print();
這是工作。 但我只想打印特定 div 的內容。 我看到了答案$("#printarea").print();
在很多地方,但這不起作用。
一些 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();'>
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);
}
如果你想在沒有額外插件的情況下做到這一點(比如printThis ),我認為這應該可行。 這個想法是有一個特殊的 div 將被打印,而其他一切都使用 CSS 隱藏。 如果 div 是 body 標簽的直接子代,這更容易做到,因此您必須將要打印的任何內容移動到這樣的 div 中。 S 所以首先創建一個 id 為print-me
的 div 作為你的 body 標簽的直接子元素。 然后使用此代碼打印 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();
});
您需要的樣式是這些:
@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; }
}
我們應該只在printing
類存在時應用@print
樣式的原因是,如果用戶通過選擇File -> Print
來打印頁面,則頁面應該正常File -> Print
。
在不使用任何插件的情況下,您可以選擇此邏輯。
$("#btn").click(function () {
//Hide all other elements other than printarea.
$("#printarea").show();
window.print();
});
上述解決方案都不是完美的。它們要么丟失 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
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();
}
下面來自 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();
})
這是一個鏈接代碼筆
使用這個庫: Print.JS
使用此庫,您可以打印 HTML 和 PDF。
<form method="post" action="#" id="printJS-form">
...
</form>
<button type="button" onclick="printJS('printJS-form', 'html')">
Print Form
</button>
<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;
}
以上功能應該在同一頁面上加載。
我更新了這個功能
現在您可以使用完整樣式打印任何標簽或頁面的任何部分
必須包含 jquery.js 文件
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='printtag("DivIdToPrint");' >
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);
}
我在這里嘗試了所有非插件方法,但都導致在內容之后打印空白頁,或者有其他問題。 這是我的解決方案:
網址:
<body>
<div id="page-content">
<div id="printme">Content To Print</div>
<div>Don't print this.</div>
</div>
<div id="hidden-print-div"></div>
</body>
查詢:
$(document).ready(function () {
$("#hidden-print-div").html($("#printme").html());
});
css:
#hidden-print-div {
display: none;
}
@media print {
#hidden-print-div {
display: block;
}
#page-content {
display: none;
}
}
首先包含標題
<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>
當您使用帶有選擇器的打印函數時,它是 print.js 的一部分,因此您需要在使用之前調用它們...
Else
window.print()
會做的
$("#btn").click(function () {
$("#printarea").print();
});
或者
$("#btn").on('click',function () {
$("#printarea").print();
});
看看這個插件
使您的代碼盡可能簡單 -> $('SelectorToPrint').printElement();
HTML:
<div class="print">
<p>Print 1</p>
<a href="#" class="js-print-link">Click Me To Print</a>
</div>
<div class="print">
<p>Print 2</p>
<a href="#" class="js-print-link">Click Me To Print</a>
</div>
查詢:
$('.js-print-link').on('click', function() {
var printBlock = $(this).parents('.print').siblings('.print');
printBlock.hide();
window.print();
printBlock.show();
});
CSS 代碼
<style type="text/css">
table {border-collapse: collapse; width: 100%; margin-bottom: 10px; width: 450px;}
table, th, td {border: 1px solid #000000;}
@media print {
.print-btn{
display: none;
}
}
</style>
HTML 代碼
<table>
<tbody>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr>
<td>Adam</td>
<td>Johnson</td>
<td>67</td>
</tr>
</tbody>
</table>
<button class="print-btn" onclick="window.print()">Print me</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.