Print
only?

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

    I had multiple images each with a button and needed to click on a button to print each div with an image. This solution works if I have disabled cache in my browser, and the image size doesn't change in Chrome:

            function printDiv(divName) {
    
            var printContents = document.getElementById(divName).innerHTML;
            w = window.open();
    
            w.document.write(printContents);
            w.document.write('' + 'window.onload = function() { window.print(); window.close(); };' + '');
    
            w.document.close(); // necessary for IE >= 10
            w.focus(); // necessary for IE >= 10
    
            return true;
        }
    
    

    Print me

提交回复
热议问题