table表格打印

网页表格打印

html body中的代码

  <table id="table" border="1" cellspacing="0">
            <thead>
            <tr>
                <th>姓名th>
                <th>性别th>
                <th>年龄th>
            tr>
            thead>
            <tbody>
            <tr>
                <td>王源td>
                <td>td>
                <td>17td>
            tr>
            <tr>
                <td>王俊凯td>
                <td>td>
                <td>18td>
            tr>
            <tr>
                <td>易烊千玺td>
                <td>td>
                <td>17td>
            tr>
            <tr>
                <td>TFBOYStd>
                <td>男团td>
                <td>5td>
            tr>
            tbody>
        table>
        <p onclick='dayin'>打印p>

script 中的代码

function dayin(){
    var tableToPrint= document.getElementById("table");//要打印的表格
    var newWin= window.open(""); //打开新的窗口
        newWin.document.write(tableToPrint.outerHTML);//将表格添加进新的窗口
        newWin.document.close(); //在IE浏览器中必须加这一句
        newWin.focus(); //在IE浏览器中必须加这一句
        newWin.print(); //打印
        newWin.close(); //关闭窗口

}

你可能感兴趣的:(table表格打印)