Print
only?

前端 未结 30 1671
暖寄归人
暖寄归人 2020-11-22 00:25

How do I print the indicated div (without manually disabling all other content on the page)?

I want to avoid a new preview dialog, so creating a new window with this

30条回答
  •  梦谈多话
    2020-11-22 00:54

    I tried many of the solutions provided.None of them worked perfectly. They either lose CSS or JavaScript bindings. I found a perfect and easy solution that neither losses CSS nor JavaScript bindings.

    HTML:

    This is a sample text for printing purpose.

    Do not print.

    Javascript:

    function printFunc() {
        var divToPrint = document.getElementById('printarea');
        var htmlToPrint = '' +
            '';
        htmlToPrint += divToPrint.outerHTML;
        newWin = window.open("");
        newWin.document.write("

    Print Page

    "); newWin.document.write(htmlToPrint); newWin.print(); newWin.close(); }

提交回复
热议问题