当前位置:编程学习 > C#/ASP.NET >>

分享js打印脚本,支持IE和Chrome,火狐还没测

/* 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);
    }
} 脚本 html --------------------编程问答-------------------- 消灭0回复,如有修正地方还请大神们指教 --------------------编程问答-------------------- 感觉jquery的局部打印 更加实用点,兼容性也高 呵呵  看情况而用把 --------------------编程问答--------------------
引用 2 楼 yyl8781697 的回复:
感觉jquery的局部打印 更加实用点,兼容性也高 呵呵  看情况而用把

要依赖jquery引用,我发的调用灰常简单 --------------------编程问答-------------------- 没人~ 自己订 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 结贴给分吧。。。 --------------------编程问答-------------------- 要有个打印预览就好点 --------------------编程问答-------------------- 局部打印用css就可以了呀

   #printdiv{
    display:none;
    position:absolute;
    z-index:999;
    left:0;
    top:0;
    }
    @@media print {   
        #wrap1,#footer1{display:none;}
        #printdiv{display:block;}
    }
--------------------编程问答-------------------- 用这个Lodop  兼容IE Chrome Firefox …… 
--------------------编程问答--------------------  window 里有个方法吧、! print() --------------------编程问答-------------------- javascript:window.print() --------------------编程问答-------------------- IE7下好像不支持,是怎么回事 --------------------编程问答-------------------- 楼主,给我发一个JS批量打印的,我现在的手上项目正好需求这个
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,