Print
only?

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

    You can use this: http://vikku.info/codesnippets/javascript/print-div-content-print-only-the-content-of-an-html-element-and-not-the-whole-document/

    Or use visibility:visible and visibility:hidden css property together with @media print{}

    'display:none' will hide all nested 'display:block'. That is not solution.

    0 讨论(0)
  • 2020-11-22 00:35

    This works well:

    <style type="text/css">
    @media print
    {
    body * { visibility: hidden; }
    #printcontent * { visibility: visible; }
    #printcontent { position: absolute; top: 40px; left: 30px; }
    }
    </style>
    

    Note that this only works with "visibility". "display" won't do it. Tested in FF3.

    0 讨论(0)
  • 2020-11-22 00:35

    I picked up the content using JavaScript and created a window that I could print in stead...

    0 讨论(0)
  • 2020-11-22 00:37

    In my case I had to print a image inside a page. When I used the solution voted, I had 1 blank page and the other one showing the image. Hope it will help someone.

    Here is the css I used:

    @media print {
      body * {
        visibility: hidden;
      }
    
      #not-print * {
        display: none;
      }
    
      #to-print, #to-print * {
        visibility: visible;
      }
    
      #to-print {
        display: block !important;
        position: absolute;
        left: 0;
        top: 0;
        width: auto;
        height: 99%;
      }
    }
    

    My html is:

    <div id="not-print" >
      <header class="row wrapper page-heading">
    
      </header>
    
      <div class="wrapper wrapper-content">
        <%= image_tag @qrcode.image_url,  size: "250x250" , alt: "#{@qrcode.name}" %>
      </div>
    </div>
    
    <div id="to-print" style="display: none;">
      <%= image_tag @qrcode.image_url,  size: "300x300" , alt: "#{@qrcode.name}" %>
    </div>
    
    0 讨论(0)
  • 2020-11-22 00:38
    <script type="text/javascript">
       function printDiv(divId) {
           var printContents = document.getElementById(divId).innerHTML;
           var originalContents = document.body.innerHTML;
           document.body.innerHTML = "<html><head><title></title></head><body>" + printContents + "</body>";
           window.print();
           document.body.innerHTML = originalContents;
       }
    </script>
    

    0 讨论(0)
  • 2020-11-22 00:38

    @Kevin Florida If you have multiple divs with same class, you can use it like this:

     <div style="display:none">
       <div id="modal-2" class="printableArea">
         <input type="button" class="printdiv-btn" value="print a div!" />
       </div>
     </div>
    

    i was using Colorbox inner content type

    $(document).on('click', '.printdiv-btn', function(e) {
        e.preventDefault();
    
        var $this = $(this);
        var originalContent = $('body').html();
        var printArea = $this.parents('.printableArea').html();
    
        $('body').html(printArea);
        window.print();
        $('body').html(originalContent);
    });
    
    0 讨论(0)
提交回复
热议问题