/** * 兼容IE8的打印頁面局部區域 * $('#printBody')爲頁面上0寬0高的div * @param printhtml 要打印的區域的innerHTML */ function printOrder(printhtml) { //清空以前打印插入的html $('#printBody').html(''); //新建一個iframe var iframe = document.createElement('iframe'); iframe.id = 'printf'; //iframe寬高設置,由於IE8下iframe不能設爲0寬,這裏寫1 iframe.style.width = '1'; iframe.style.height = '1'; iframe.style.border = "none"; //將iframe插入到printBody裏 document.getElementById("printBody").appendChild(iframe); setTimeout(function () { if (document.all) { //IE下,在iframe裏調用打印 iframe.contentDocument.write("<script type='text/javascript'> window.onload=function(){ document.execCommand('print'); } </script>" + printhtml); iframe.contentDocument.close(); } else { //其它瀏覽器直接調用 iframe.contentDocument.write(printhtml); iframe.contentDocument.close(); iframe.contentWindow.focus(); iframe.contentWindow.print(); } }, 100) };