简体   繁体   中英

JavaScript WIndow Print - I want it to print a specific div

I have this nifty shopping helper at http://www.spacerock.com/shoppingplanner.php - click a meal and the ingredients appear in the shopinglist div. I have the print window button at the top but it does not print that div. How can I designate it to print that div? The code for the button is I know it's probably very simple; I'm a jQuery person and need to learn PURE JS. Thanks in advance for your help and education.

Use This JS:
to print specific DIV with ID='content'

<input style="width:75px" type="button" name="printreport" id="printreport" value="Print List" onclick="printpage()">

function printpage()
    {
        var originalContents = document.body.innerHTML;
        var printReport = document.getElementById('content').innerHTML;
        document.body.innerHTML = printReport;
        window.print();
        document.body.innerHTML = originalContents;
    }

Hope this helps..

You could try using the printThis Jquery plugin:

$('SelectorToPrint').printThis();

JSFIDDLE https://jsfiddle.net/seadonk/df4tzcq5/

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