当我在开发人员工具中运行JS时,它显示了来自innerHTML的所有数据,但是print只显示带有日期的空白页面。任何反馈都表示赞赏。
function PrintThisPage(sectionName) {
var sOption = "toolbar=yes,location=no,directories=yes,menubar=yes,";
sOption += "scrollbars=yes,resizable=yes,status=yes,width=750,height=600,left=100,top=25";
var sWinHTML = document.getElementById(sectionName).innerHTML;
var winprint = window.open("", "", sOption);
winprint.document.open();
winprint.document.write('<html><head>');
winprint.document.write('<LINK href="../stylesheets/main.css" type="text/css" rel="stylesheet">');
winprint.document.write('<LINK href="../stylesheets/FormEdit.css" type="text/css" rel="stylesheet">');
winprint.document.write('</head>')
winprint.document.write('<body style="display:none">');
winprint.document.write(sWinHTML);
arrInputs = winprint.document.getElementsByTagName('input');
for (i = 0; i < arrInputs.length; i++) {
if (arrInputs[i].type == "image" || arrInputs[i].type == "button" || arrInputs[i].type == "submit") {
arrInputs[i].style.display = 'none';
}
}
arrInputs = winprint.document.getElementsByTagName('td');
for (i = 0; i < arrInputs.length; i++) {
arrInputs[i].onmouseover = null;
}
winprint.document.write('</body></html>');
winprint.document.body.style.display = '';
winprint.document.close();
if (window.print) {
winprint.print();
}
else {
alert("Sorry, the print ready feature is only available in certain browsers.");
} // end - if (window.print)
}
修改
imgPrint.Attributes["onclick"] = string.Format("PrintThisPage('{0}');", divDataContent.ClientID);
当我在chrome中运行它时,我得到一个异常:{System.Web.HttpException(0x80004005):无法获取divDataContent的内部内容,因为内容不是文字。
修改2
我在IE中收到相同的错误,但功能仍然有效。
编辑3
显然Chrome不喜欢document.write,因此我将其修改为document.body / head.innerHTML。
谢谢大家。