我为网页编写了基于JavaScript的打印功能。它从页面上一个隐藏的div中提取HTML,将其呈现到一个新窗口中并触发打印对话框。
用户可以通过一个带有
实现方式是打开一个新的浏览器窗口并在其中附加文档:
更新:这是按钮的外观:
用户可以通过一个带有
onclick="printTheThing()"
事件的使用该选项。我知道例如屏幕阅读器在JavaScript方面存在一些注意事项。我想知道这样的特性会阻止多少盲人或视力受损的人使用该功能。实现方式是打开一个新的浏览器窗口并在其中附加文档:
function printTheThing() {
var dispSettings = "toolbar=yes,location=no,directories=yes,menubar=yes,"
+ "scrollbars=yes,width=550,height=400",
html = $('#theDivToPrint').html(),
printWindow = window.open("", "", dispSettings),
doc = printWindow.document;
doc.open();
try {
doc.write('<html><head>');
doc.write('<title>' + title + '</title>');
doc.write('</head><body>');
doc.write(html);
doc.write('</body></html>');
} finally {
doc.close();
}
printWindow.focus();
printWindow.print();
}
更新:这是按钮的外观:
<button type="button" onclick="printTheThing()">Print the thing!</button>
此外,我正在使用CSS将按钮替换为图像。我已经使用Firefox插件“Fangs”测试了该按钮。它建议屏幕阅读器可以完美地读出原始按钮文本。但是Fangs不提供任何交互功能,因此我无法用它来测试打印效果。