最近我在这里看到了这个类似的问题。如果你想去掉 URL 地址,你可以新建一个窗口,将可打印的内容复制到那里并打印新窗口,然后关闭它。那么原来的地址会被替换成about:blank
.
function printpage() {
var styles = document.getElementsByTagName('style');
var style = '';
for (var i=0; i<styles.length; i++) {
style += styles[i].innerHTML;
}
var data = document.getElementById('print_content').innerHTML;
data += '<br/><button onclick="window.print()" class="noprint">Print the Report</button>';
data += '<br/><button onclick="window.close()" class="noprint">Close Preview</button>';
data += '<style type="text/css" media="print"> .noprint {visibility: hidden;}</style><style type="text/css">'+ style +' </style>';
myWindow = window.open('', '', 'width=800,height=600');
myWindow.innerWidth = screen.width;
myWindow.innerHeight = screen.height;
myWindow.screenX = 0;
myWindow.screenY = 0;
myWindow.document.body.innerHTML = data;
myWindow.focus();
}
jsfiddle 代码+结果页面(适用于 IE7+)