javascript打印网页的特定内容(以打印table为例)

参考网址:
http://stackoverflow.com/questions/21375398/how-to-print-table-using-javascript
http://stackoverflow.com/questions/2555697/window-print-not-working-in-ie


html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网页打印示例title>
head>

<body>
    <input type="button" id="print" value="打印"/>
    <table border="1" cellpadding="3" id="printTable" >
        <tbody><tr>
            <th>First Nameth>
            <th>Last Nameth>
            <th>Pointsth>
        tr>
        <tr>
            <td>Jilltd>
            <td>Smithtd>
            <td>50td>
        tr>
        <tr>
            <td>Evetd>
            <td>Jacksontd>
            <td>94td>
        tr>
        <tr>
            <td>Johntd>
            <td>Doetd>
            <td>80td>
        tr>
        <tr>
            <td>Adamtd>
            <td>Johnsontd>
            <td>67td>
        tr>
        tbody>
    table>

    <script>
        window.onload = function()
        {
            var btnPrint = document.getElementById("print");//“打印”按钮
            btnPrint.onclick = function()//为“打印”按钮添加点击事件
            {
                printTable();//打印表格
            }
        }

        //打印表格
        function printTable()
        {
            var tableToPrint = document.getElementById('printTable');//将要被打印的表格
            var newWin= window.open("");//新打开一个空窗口
            newWin.document.write(tableToPrint.outerHTML);//将表格添加进新的窗口
            newWin.document.close();//在IE浏览器中使用必须添加这一句
            newWin.focus();//在IE浏览器中使用必须添加这一句

            newWin.print();//打印
            newWin.close();//关闭窗口
        }
    script>
body>
html>


你可能感兴趣的:(Web)