[英]How to get only content specific height and width in print using javascript
我正在開發pos系統,但是當我打印賬單時,它會打印在整個頁面上,我只希望它只占用特定的寬度,並且只占用div覆蓋的那么高。
這是我的代碼
<html>
<head>
<title>div print</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js" />
<script type="text/javascript">
function printdiv(printpage) {
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr+footstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
</head>
<body>
<input name="b_print" type="button" class="ipt" onClick="printdiv('div_print');" value=" Print ">
<div id="div_print">
<h1 style="Color:Red">this will print</h1>
</div>
</body>
</html>
理想的解決方案是在指定尺寸限制的地方使用單獨的CSS進行打印。 您可以使用特定於打印的CSS樣式表,並隱藏除您要打印的內容以外的所有內容。 將您的元素分為兩類,一類不打印,另一類將:
<div class="no-print">Ignore me</div><div class="something-else">Print me!
</div>
print類中的元素應保留,而被忽略的元素不應打印。
<style type="text/css" media="print">
.no-print { display: none; }
</style>
作為警告,請小心“ document.all.item”,好像找到了多個元素,它將返回一系列可能導致問題的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.