javascript - 如何使用 javascript 打印弹出窗口的内容?

标签 javascript jquery

我有一个使用 bPopUp 的网页使用 jQuery 弹出模式窗口。

那个窗口的内容正是我想要打印的(主页上没有其他内容)。

我已经尝试在弹出窗口上链接一个按钮来运行命令 window.print(); 但这只会打印整个页面,显示弹出窗口遮住了页面的其余部分。

有谁知道我怎么只能打印弹出窗口的内容而不能打印其他内容?

谢谢

最佳答案

示例 HTMP

<div id='DivIdToPrint'>
    <p>This is a Popup which needs to be printed.</p>
</div>

<div>
    <p>Do not print.</p>
</div>

<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);
}

为您的 Popup div 提供一个 ID 并使用上面的示例。 您需要提供内联样式才能准确打印弹出窗口。

关于javascript - 如何使用 javascript 打印弹出窗口的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40241409/

相关文章:

javascript - ES6 类是如何编译的?

javascript - 向/从子元素事件触发的表格单元格添加/删除类

javascript - 从 anchor 内的 span 元素到列表的 DOM 遍历

javascript - 选择没有某些后代的元素内容

javascript - Flex 布局 - 单元格在 chrome 中部分折叠

javascript - 这个js外壳是如何工作的?

javascript - jQuery 多重选择器,带有 $(this) 用于下拉菜单和文本字段

javascript - x-editable textarea,html值在编辑时消失

javascript - 将输入的换行符转换为 <br/> 标签

javascript - 在 contenteditable div 中突出显示的文本周围添加 html 标签