js打印

function printThis(){
//    console.log(jQuery("#if1",parent.window.document));
//    alert(window.parent.document.frames['if1']);
   if (!!window.ActiveXObject || "ActiveXObject" in window){
//        alert(window.parent.frames['if1']);
//        alert("IE");
        var bdhtml=window.document.body.innerHTML;
        var sprnstr="<!--startprint-->"; //开始打印标识字符串有17个字符
        var eprnstr="<!--endprint-->"; //结束打印标识字符串
        var prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); //从开始打印标识之后的内容
        var prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); //截取开始标识和结束标识之间的内容
        window.document.body.innerHTML=prnhtml; //把需要打印的指定内容赋给body.innerHTML
        window.parent.document.frames['if1'].focus();
//        jQuery("#if1",window.parent.document).focus();
//        window.frames['if1'].focus();
//        document.execCommand('print', false, null);
        print(); //调用浏览器的打印功能打印指定区域
        window.document.body.innerHTML=bdhtml;//重新给页面内容赋值;
    }else{
//        $("#wrap").jqprint();
//        alert(window.frames['if1']);
        var bdhtml=window.document.body.innerHTML;
        var sprnstr="<!--startprint-->"; //开始打印标识字符串有17个字符
        var eprnstr="<!--endprint-->"; //结束打印标识字符串
        var prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); //从开始打印标识之后的内容
        var prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); //截取开始标识和结束标识之间的内容
        window.document.body.innerHTML=prnhtml; //把需要打印的指定内容赋给body.innerHTML
        window.print(); //调用浏览器的打印功能打印指定区域
        window.document.body.innerHTML=bdhtml;//重新给页面内容赋值;
    }
}
原文地址:https://www.cnblogs.com/web520/p/6879386.html