Javascript 从车身打印所选零件
我想打印只包含所选零件的主体 单击此方法后,页面对齐方式将更改 //应用程序Javascript 从车身打印所选零件,javascript,html,Javascript,Html,我想打印只包含所选零件的主体 单击此方法后,页面对齐方式将更改 //应用程序 家 页面 接触 售价24.78美元 产品状态即将达到 //js printDiv(){ //获取div的HTML var divElements=document.getElementById('payment-receive').innerHTML; //获取整个页面的HTML var oldPage=document.body.innerHTML; //仅用div的HTML重置页面的HTML documen
- 家
- 页面
- 接触
售价24.78美元
产品状态即将达到
//js
printDiv(){
//获取div的HTML
var divElements=document.getElementById('payment-receive').innerHTML;
//获取整个页面的HTML
var oldPage=document.body.innerHTML;
//仅用div的HTML重置页面的HTML
document.body.innerHTML=“+divElements+”;
//打印页
window.print();
//还原原始HTML
document.body.innerHTML=旧页;
}
单击方法前后的对齐应保持不变您可以使用
@媒体打印{
/*在媒体打印上隐藏此元素*/
.标题{
显示:无;
}
}
- 家
- 页面
- 接触
售价24.78美元
产品状态即将达到
- 家
- 页面
- 接触
售价24.78美元
产品状态即将达到
使用javascript无jquery打印所选零件
var el=document.getElementById('payment-receive');
如果(el){
el.addEventListener('click',print_div,false);
}
函数print_div(){
var content=document.getElementById('payment-receive').innerHTML;
var mywindow=window.open();
mywindow.document.write('Print'+content+'');
mywindow.print();
document.body.innerHTML=内容;
mywindow.close();
返回true;
}
根据答案稍加修改:
var el=document.getElementById('payment-receive');
如果(el){
el.addEventListener('click',print_div,false);
}
函数print_div(){
var content=document.getElementById('payment-receive').innerHTML;
var mywindow=window.open();
mywindow.document.write('Print'+window.getSelection()+'');
mywindow.print();
document.body.innerHTML=内容;
返回true;
}
- 家
- 页面
- 接触
售价24.78美元
产品状态即将达到
我遇到了同样的问题-有一个包含用户数据的表单。我必须打印表单,但也要防止打印不必要的数据,或者说只打印页面中包含数据的特定部分
我是怎么解决的
1。对于简单案例
隐藏所有不需要的元素,并在其余部分调用window.print()
printForm(){
document.getElementById('element1').style.display='None'
document.getElementById('element2').style.display='None'
document.getElementById('element2').style.display='None'
window.print()
}
2。对于复杂情况
现在在复杂dom中可能有一个问题,在我的例子中它很简单,现在让我们看看如何在复杂dom结构中解决同样的问题
HTML
<body>
<div id="content">
<!-- Content that has to be printed -->
</div>
<div id="print-content">
<!-- Will be used for printing purpose -->
</div>
</body>
我认为最好使用css打印样式来解决这个问题。请检查此使用@media print
<div class="header">
<ul>
<li>Home</li>
<li>Page</li>
<li>Contact</li>
</ul>
</div>
<div id="payment-receipt" name='clickf'>
<div>
<p>Price$24.78</p>
<p>Product Status About to reach </p>
</div>
</div>
var el = document.getElementById('payment-receipt');
if(el){
el.addEventListener('click', print_div, false);
}
function print_div(){
var content = document.getElementById('payment-receipt').innerHTML;
var mywindow = window.open();
mywindow.document.write('<html><head><title>Print</title></head><body >'+content+'</body></html>');
mywindow.print();
document.body.innerHTML =content;
mywindow.close();
return true;
}
printForm(){
document.getElementById('element1').style.display='None'
document.getElementById('element2').style.display='None'
document.getElementById('element2').style.display='None'
window.print()
}
<body>
<div id="content">
<!-- Content that has to be printed -->
</div>
<div id="print-content">
<!-- Will be used for printing purpose -->
</div>
</body>
let contentEl = document.getElementById('content')
let printEl = document.getElementById('print-content')
printEl.appencChild(contentEl.innerHtml)
contentEl.style.display = "None"
window.print()