网页中Div的打印

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>DIV打印Demo</title>
        <!--jqueryCDN-->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    </head>
    <body>
        <div id="printdiv">
            <div style="text-align: center;">
                <h3>沁园春·雪</h3>
                <p>北国风光,千里冰封,万里雪飘。</p>
                <p>望长城内外,惟余莽莽;大河上下,顿失滔滔。</p>
                <p>山舞银蛇,原驰蜡象,欲与天公试比高。</p>
                <p>须晴日,看红装素裹,分外妖娆。</p>
                <p>江山如此多娇,引无数英雄竞折腰。</p>
                <p>惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。</p>
                <p>一代天骄,成吉思汗,只识弯弓射大雕。</p>
                <p>俱往矣,数风流人物,还看今朝。</p>
            </div>
        </div>
        <button id="print">打印</button>
        <div style="text-align: center;">
            <p>这是一段废话不打印,hahahahahaahahahahahahahahahahahahaha</p> 
        </div>
    </body>
    <script>
        $("#print").click(function(){
            var frame1=$('<iframe />');
            frame1[0].name="frame1";
            //隐藏构造的iframe
            frame1.css({ "display": "none" });
            $("body").append(frame1);
            var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow :
                    frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head>');
            frameDoc.document.write('<title>沁园春·雪</title>');
            frameDoc.document.write('</head><body>');
            frameDoc.document.write($("#printdiv").html());
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            
            window.frames["frame1"].focus();
            window.frames["frame1"].print();
            frame1.remove();
        })
    </script>
</html>

 

转载于:https://www.cnblogs.com/MrZheng/p/7766778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值