我在使用 window.print()
时遇到了一个很奇怪的问题。
我有一个页面需要有一个不同于通常显示给用户的打印布局。因此,我创建了一个叠加层,当用户单击 print
按钮然后触发 window.print()
时会显示该叠加层。一切正常,除了打印窗口不止一次显示的某些未知原因。如果我第一次点击打印按钮,我得到打印窗口 2 次,第二次我得到 4 次。
HTML:
<div id="printDiv" style="position: fixed; overflow: auto; top: 0px; width: 100%; height: 100%; background: white; z-index: 9999;display: none;">
The print page content
</div>
JavaScript:
function show_print(){
$('body').css('overflow', 'hidden');
$('html,body').animate({ scrollTop: 0 }, 'fast', function () {
window.print();
});
}
知道是什么原因造成的吗?
最佳答案
我相信这可能是因为您选择了 two elements因此在迭代 jQuery 选择器中的每个元素时触发两次。
因此,通过将其调整为仅 $('html')
标记:
$('html').animate({ scrollTop: 0 }, 'fast', 'swing', function () {
window.print(); //Prints once
});
fiddle :http://jsfiddle.net/ThFvD/
关于javascript - window.print() 上的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19584272/