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>
相關文章
相關標籤/搜索