window.print()打印网页中指定内容

<!DOCTYPE html>
<html>
    <head>
        <meta charset=" utf-8">
        <meta name="author" content="http://www.jb51.net/" />
        <title>指定被打印的内容</title>
        <script language="javascript"> 
            function printdiv(printpage) 
            { 
                var newstr = printpage.innerHTML; 
                var oldstr = document.body.innerHTML; 
                document.body.innerHTML =newstr; 
                window.print(); 
                document.body.innerHTML=oldstr; 
                return false; 
            } 
            window.onload=function()
            {
                var bt=document.getElementById("bt");
                var div_print=document.getElementById("div_print");
                bt.onclick=function()
                {
                    printdiv(div_print);
                }
            }
        </script> 
    </head> 
    <body> 
        <div id="div_print"> 
            <h1 style="Color:Red">这是要被打印的内容</h1> 
        </div>
        <div style="Color:Red">欢迎您</div> 
            <input name="print" type="button" id="bt" value="点击打印" /> 
    </body> 
</html>

 

posted @ 2019-03-18 15:35  发福大叔  阅读(2746)  评论(0编辑  收藏  举报