[英]Print specific elements on page?
I'm looking to print specific text elements of a page. 我正在寻找打印页面的特定文本元素。 I currently have 10 cards on a single page and when a user chooses to print a specific card, I only want the chosen card to print.
我目前在一页上有10张卡片,当用户选择打印特定的卡片时,我只希望选择的卡片进行打印。 I was using
javascript:window.print()
, but found that it prints all the cards. 我使用的是
javascript:window.print()
,但发现它可以打印所有卡片。 However I found a snippet of code (below) that does what I want, but strips off the logo. 但是我发现了下面的代码片段可以满足我的要求,但是却去除了徽标。 I've try adding
document.getElementById('logo')
but it doesn't work. 我尝试添加
document.getElementById('logo')
但是它不起作用。 Not sure how to add the logo as a printable area as well. 也不确定如何将徽标添加为可打印区域。 Is there a jQuery method as well that could handle this without using a plugin?
是否还有一个无需使用插件即可处理此问题的jQuery方法?
HTML 的HTML
<div class="container">
<img id="logo" src="picture.jpg">
<div class="card" id="card-a">Text <a href="" onclick="printDiv('card-a)">Print</a></div>
<div class="card" id="card-b">Text <a href="" onclick="printDiv('card-b)">Print</a></div>
<div class="card" id="card-c">Text <a href="" onclick="printDiv('card-c)">Print</a></div>
</div>
JS JS
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
Just add it to the HTML before you overwrite the body: 只需在覆盖主体之前将其添加到HTML中即可:
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
var logoContent = document.getElementById("logo").outerHTML;
document.body.innerHTML = logoContent + printContents;
window.print();
document.body.innerHTML = originalContents;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.