Print a div content using Jquery

前端 未结 13 2236
栀梦
栀梦 2020-11-30 05:22

I want to print the content of a div using jQuery. This question is already asked in SO, but I can\'t find the correct (working) answer.

This is is my HTML:

13条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 06:02

    Some jQuery research has failed, so I moved to JavaScript (thanks for your suggestion Anders).

    And it is working well...

    HTML

    This is a sample text for printing purpose.

    Do not print.

    JavaScript

    function printDiv() 
    {
    
      var divToPrint=document.getElementById('DivIdToPrint');
    
      var newWin=window.open('','Print-Window');
    
      newWin.document.open();
    
      newWin.document.write(''+divToPrint.innerHTML+'');
    
      newWin.document.close();
    
      setTimeout(function(){newWin.close();},10);
    
    }
    

提交回复
热议问题