js 打印 HTML 隐藏不想打印的元素

function printPagebyJS(domObj) {
    pagesetup_null();
    if (domObj) {
        var allHtml = window.document.body.innerHTML;
        var printHtml = domObj.outerHTML;
        window.document.body.innerHTML = printHtml;
        window.print();
        window.document.body.innerHTML = allHtml;
    } else {
        window.print();
    }
    pagesetup_default();
}

var hkey_root, hkey_path, hkey_key
hkey_root = "HKEY_CURRENT_USER"
hkey_path = "\\software\\Microsoft\\Internet Explorer\\PageSetup\\"

//设置网页打印的页眉页脚边距为空
function pagesetup_null() {
        try {
            var RegWsh = new ActiveXObject("WScript.Shell");
            hkey_key = "header";
            RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
            hkey_key = "footer";
            RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
            hkey_key = "margin_left";
            RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
            hkey_key = "margin_right";
            RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
            hkey_key = "margin_top";
            RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
            hkey_key = "margin_bottom";
            RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
        } catch (e) {}
    }

//设置网页打印的页眉页脚边距为默认值
function pagesetup_default() {
    try {
        var RegWsh = new ActiveXObject("WScript.Shell");
        hkey_key = "header";
        RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b页码,&p/&P");
        hkey_key = "footer";
        RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&u&b&d");
        hkey_key = "margin_left";
        RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
        hkey_key = "margin_right";
        RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
        hkey_key = "margin_top";
        RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
        hkey_key = "margin_bottom";
        RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
    } catch (e) {}

}

-------------------------------------

jsp:示例

    <body>       
        <input id="dayinDiv" name="dayinDiv" type="button" class="tab" value="打印" οnclick="printTure();">
        <div id="printDiv">
            --- 打印内容 ----
        </div>

        <script type="text/javascript">
        
            /** *传入的IDS为需要屏蔽的元素id数组 */

/*             function print(IDS) { //将需要屏蔽的元素隐藏
                for (var i = 0; i < IDS.length; i++) {
                    var target = document.getElementById(IDS[i]);
                    target.style.display = "none";
                }
                var imagesarray = window.document.images
                for (var i = 0; i < imagesarray.length; i++) {
                    imagesarray[i].style.display = "none";
                }
                //打印
                window.print();
                //显示屏蔽的元素
                for (var i = 0; i < IDS.length; i++) {
                    var target = document.getElementById(IDS[i]);
                    target.style.display = "block";
                }
                for (var i = 0; i < imagesarray.length; i++) {
                    imagesarray[i].style.display = "block";
                }
            } */
            
            function printTure() {
                printPage(document.getElementById("printDiv")); 
            }
        </script>
    </body>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值