/* js打印功能,使用chrome浏览器体验最佳
参数str可以使用id或者html等,如:printWork("divid");printWork("<div>TEST</div>") */
function printWork(str) {
var orderhtml = "";
if (document.getElementById(str)) { orderhtml = document.getElementById(str).outerHTML; }
else orderhtml = str;
/* 创建iframe */
var headobj = document.getElementsByTagName("head").item(0); //提取head
printFrame = document.getElementById("lldxi_printRegionFrame_2012_0112");
if (printFrame) { document.body.removeChild(printFrame); }
printFrame = document.createElement("iframe");
printFrame.setAttribute("src", "about:blank");
printFrame.setAttribute("id", "lldxi_printRegionFrame_2012_0112");
printFrame.setAttribute("marginheight", "0");
printFrame.setAttribute("marginwidth", "0");
printFrame.style.display = "none";
document.body.appendChild(printFrame);
if (window.ActiveXObject)//ie
{
var htmlobj = printFrame.contentWindow.document.createElement("html"); var bodyobj = printFrame.contentWindow.document.createElement("body");
bodyobj.innerHTML = orderhtml; htmlobj.appendChild(headobj.cloneNode(true)); htmlobj.appendChild(bodyobj);
printFrame.contentWindow.document.appendChild(htmlobj); printFrame.contentWindow.document.execCommand("Print", true);
}
else {
var htmlstr = "<html>" + headobj.outerHTML + "<body>" + orderhtml + "<script type=\"text/javascript\">window.print();<\/script><\/body>" + "<\/html>";
printFrame.contentWindow.document.write(htmlstr);
}
}
参数str可以使用id或者html等,如:printWork("divid");printWork("<div>TEST</div>") */
function printWork(str) {
var orderhtml = "";
if (document.getElementById(str)) { orderhtml = document.getElementById(str).outerHTML; }
else orderhtml = str;
/* 创建iframe */
var headobj = document.getElementsByTagName("head").item(0); //提取head
printFrame = document.getElementById("lldxi_printRegionFrame_2012_0112");
if (printFrame) { document.body.removeChild(printFrame); }
printFrame = document.createElement("iframe");
printFrame.setAttribute("src", "about:blank");
printFrame.setAttribute("id", "lldxi_printRegionFrame_2012_0112");
printFrame.setAttribute("marginheight", "0");
printFrame.setAttribute("marginwidth", "0");
printFrame.style.display = "none";
document.body.appendChild(printFrame);
if (window.ActiveXObject)//ie
{
var htmlobj = printFrame.contentWindow.document.createElement("html"); var bodyobj = printFrame.contentWindow.document.createElement("body");
bodyobj.innerHTML = orderhtml; htmlobj.appendChild(headobj.cloneNode(true)); htmlobj.appendChild(bodyobj);
printFrame.contentWindow.document.appendChild(htmlobj); printFrame.contentWindow.document.execCommand("Print", true);
}
else {
var htmlstr = "<html>" + headobj.outerHTML + "<body>" + orderhtml + "<script type=\"text/javascript\">window.print();<\/script><\/body>" + "<\/html>";
printFrame.contentWindow.document.write(htmlstr);
}
}
display:none;
position:absolute;
z-index:999;
left:0;
top:0;
}
@@media print {
#wrap1,#footer1{display:none;}
#printdiv{display:block;}
}
window.print();我以为有什么特殊的地方呢.