我正在尝试在IE10中打印动态创建的iframe。它既不会引发错误,也不会起作用。下面是我的代码。
function PrintVisitSlip() {
var f = document.getElementById('frmPrintVisitSlip');
var ifrm = document.createElement("iframe");
ifrm.src = 'about:blank';
ifrm.style.display = "none";
document.body.appendChild(ifrm);
var _styleCSS = '<style type="text/css">p{border-bottom: white;}.Head{font-size: 9pt;font-weight: bold;}'
+ 'td{border-bottom: 1px solid black;height: 8px;padding: 0px 0px 0px 0px;margin: 0 0 0 0;font-size: 7pt;padding: 1px 0 1px 0;}'
+ '.LC{width: 125px;text-align: left;padding-left: 4px;}.RC{width: 21px;text-align: right;padding-right: 3px;}'
+ '.LC1{width: 80px;text-align: left;padding-left: 4px;font-size: 6.5pt;}.RC1{width: 18px;text-align: right;}</style>';
ifrm.contentWindow.document.open('text/html', 'replace');
ifrm.contentWindow.document.write(_styleCSS + f.outerHTML);
ifrm.contentWindow.document.close();
var iw = ifrm.contentWindow || ifrm;
iw.focus();
iw.print();
return false;
}
此代码在chrome中可以正常工作。但是在IE10中,单击“打印”按钮后看不到任何打印窗口。
尝试readyState
在iframe
的文档为时打印它complete
,就像open.write
IE下的异步打印一样。而且,ifrm.style.display = "none"
防止打印框架,因此整个窗口将被打印。
以下代码已通过IE10验证。它可能不适用于其他浏览器。
<!DOCTYPE html>
<head>
<title>main</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script>
function Go() {
var ifrm = document.createElement("iframe");
ifrm.src = 'about:blank';
ifrm.style.borderWidth = 0;
ifrm.style.width = 0;
ifrm.style.height = 0;
document.body.appendChild(ifrm);
ifrm.contentWindow.document.open("text/html", "replace");
ifrm.contentWindow.document.onreadystatechange = function () {
if (ifrm.contentWindow.document.readyState === "complete") {
ifrm.contentWindow.document.body.onafterprint = function () {
ifrm.removeNode(true);
}
ifrm.contentWindow.document.body.focus();
ifrm.contentWindow.print();
}
}
ifrm.contentWindow.document.write("<b>Hello</b>, world!");
ifrm.contentWindow.document.close();
}
</script>
</head>
<body>
<button onclick="Go()">Go</button>
</body>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句