简体   繁体   中英

Print only certain part of the <div> content

I have the following JavaScript code to print a content

<script type="text/javascript">

    function Print(print)
    {
        Popup($(print).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open("", "print");
        mywindow.document.write("<html><head><title>Report</title>");
        mywindow.document.write("</head><body>");
        mywindow.document.write("<div align='center'>");
        mywindow.document.write(data);
        mywindow.document.write("</div>");
        mywindow.document.write("</body></html>");

        mywindow.print();
        mywindow.close();

        return true;    }

</script>

And in my HTML I have the following code:

   <html>
    <body>
    <p><a href="javascript:Print('#print')">Print</a></p>
    <div id="print">
    <h1>Report</h1>
    <table>
    <tr><th>Date</th><th>Remarks</th><th>Amount</th><th>Actions</th></tr>
    <tr>
    <td>10/11/2014</td>
    <td>Hello</td>
    <td>$14</td>
    <td>Edit</td>
    </tr>
    </table>
    </div>
</body>
</html>

I want to print only the first 3 columns. I do not want to print the 4th Column "Actions". When I try to print out the content by using the above code it prints all the 4 columns. Please help me with the following.

You can do that with jquery.

$(document).ready(function(){
    $('.tabel').find(':nth-child(4)').hide();
});

JSFIDDLE HERE

You can simply put the style to display none with id for that td's

Inside HTML:

....
<th id="act">Actions</th>
....
<td id="act">Edit</td>

Inside JS:

mywindow.document.write("<html><head><style>#act{display:none;}</style><title>Report</title>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM