Print
only?

前端 未结 30 2202
暖寄归人
暖寄归人 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:50

    The printDiv() function came out a few times, but in that case, you loose all your binding elements and input values. So, my solution is to create a div for everything called "body_allin" and another one outside the first one called "body_print".

    Then you call this function:

    function printDiv(divName){
    
        var printContents = document.getElementById(divName).innerHTML;
    
        document.getElementById("body_print").innerHTML = printContents;
    
        document.getElementById("body_allin").style.display = "none";
        document.getElementById("body_print").style.display = "";
    
        window.print();
    
        document.getElementById("body_print").innerHTML = "";
        document.getElementById("body_allin").style.display = "";
        document.getElementById("body_print").style.display = "none";
    
    }
    

提交回复
热议问题