我有一些JavaScript用于打开一个新窗口来打印部分页面我正在使用window.open函数,我已经将页面标题设置为window.open第二个参数(参见下面的代码),但标题显示为'about:blank'而不是'test'
<script type="text/javascript">
function printPartOfPage(elementId) {
var printContent = document.getElementById(elementId);
var windowUrl = 'about:blank';
var printWindow = window.open(windowUrl, 'test', 'left=50000,top=50000,width=0,height=0');
printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
}
</script>
注意: 我知道这个问题可能会重复,但我会尝试所有答案,但这对我不起作用:
1 - 将open()的第一个参数设置为'about:blank'
2- printWindow.document.title ='test'
结果如下所示
答案 0 :(得分:2)
问题是你提出
printWindow.document.title = 'title'
<{1>}之前的
解决方案在printWindow.document.write
printWindow.document.title
printWindow.document.write
答案 1 :(得分:1)
您可以data URI
.html
document
<title>
替换about:blank
document.write()
,print()
。在<script>
代码集中调用<body>
作为function printPartOfPage(elementId) {
var printContent = document.getElementById(elementId);
var windowUrl = "data:text/html,<!DOCTYPE html><html><head><title>test</title></head><body>" + printContent.innerHTML + "<script>print()<\/script></body></html>";
var printWindow = window.open(windowUrl, "test", 'left=50000,top=50000,width=0,height=0');
}
元素的最后一个子元素。
Blob()
plnkr http://plnkr.co/edit/ZfqHgNnjAkunJeryn71Y?p=preview
使用URL.createObjectURL()
,window.open()
将Blob URL
网址设为function printPartOfPage(elementId) {
var printContent = document.getElementById(elementId);
var html = "<!DOCTYPE html><html><head><title>test</title></head><body>" + printContent.innerHTML + "<script>print()<\/script></body></html>";
var blob = new Blob([html], {type:"text/html"});
var windowUrl = URL.createObjectURL(blob);
var printWindow = window.open(windowUrl, "test", 'left=50000,top=50000,width=0,height=0');
window.onfocus = function() {
URL.revokeObjectURL(windowUrl);
printWindow.close();
}
}
window.onload = function(){
[].forEach.call(document.querySelectorAll('#theTable tr'), x => console.log(x.id));
};