简体   繁体   English

如何打印特定的 div 区域?

[英]How can I print specific div area?

I am adding print function (as a button) to my project.我正在将 print function (作为按钮)添加到我的项目中。 Specifically when user pressed "print" button, then user can print specific div area (not only div content) of calendar.具体来说,当用户按下“打印”按钮时,用户可以打印日历的特定 div 区域(不仅是 div 内容)。

I tried following code but it is not printing the whole div area and the second problem is I can not do anything on browser after running this code(the page is dead).我尝试了以下代码,但它没有打印整个 div 区域,第二个问题是运行此代码后我无法在浏览器上执行任何操作(页面已死)。 It prints also only the div content by the way.顺便说一下,它也只打印 div 内容。

       on(dijit.byId("printCalendarButton"), "click", function(event) {
           var calendarContainer = dojo.byId("calendarContainer").innerHTML;   
           var originalContents = document.body.innerHTML;
           document.body.innerHTML = calendarContainer;
           window.print();
           document.body.innerHTML = originalContents;
       });

I also tried with css,but it prints also only a part of the page:我也尝试使用 css,但它也只打印页面的一部分:

@media print {
  body * {
    visibility: hidden;
  }
  #calendarContainer * {
    visibility: visible;
  }
  #calendarContainer {
    position: absolute;
    left: 0;
    top: 0;
  }
}

Problem by printing:打印问题: 打印图像的问题

Is there any good Ideas?有什么好的想法吗? Thanks!谢谢!

Here's a basic printing arrangement you need for printing a single element with its content.这是打印单个元素及其内容所需的基本打印安排。 Note specifically the CSS, when you're hiding all the elements inside the body with body * , this will also hide the elements to print.请特别注意 CSS,当您使用body *隐藏正文内的所有元素时,这也会隐藏要打印的元素。

To bring the printable elements to the print view, they all must be shown.要将可打印元素带到打印视图,它们都必须显示。 This is made with #printable, #printable * {display: block;} rule.这是使用#printable, #printable * {display: block;}规则制作的。 #printable * here is important, since the body * rule has hidden all the elements inside body by the type. #printable *这里很重要,因为body *规则已按类型隐藏了body内的所有元素。 Because types have a higher specifity , we can't override that with any other selector.因为类型具有更高的特异性,所以我们不能用任何其他选择器覆盖它。

To make the body * to work as intended, ie to hide everything on the page, there must not be orphanned textnodes in body , everything must be enclosed in elements.为了使body *按预期工作,即隐藏页面上的所有内容, body中不能有孤立的文本节点,所有内容都必须包含在元素中。

 const but = document.querySelector('#printButton'); but.addEventListener('click', e => { window.focus(); window.print(); });
 @media print { body * { display: none; } #printable, #printable * { display: block; } }
 <p>Some text visible on the screen only.</p> <div id="printable"> <p>This element is printed only (with its wrapper), but is visible also on the screen.</p> </div> <button id="printButton">Print</button> <p>More text visible on the screen only.</p>

If the content shows up on the paper only partially, you've to scale it to fit to the paper, or split it over multiple pages.如果内容仅部分显示在纸上,则必须将其缩放以适合纸面,或将其拆分为多个页面。 It's also possible, that the existing rules are defining margins and other stuff.现有规则也有可能正在定义边距和其他内容。 If that's the case, you've to override such rules in the printing rules too.如果是这种情况,您也必须在打印规则中覆盖这些规则。 Media queries don't exclude existing rules, they're still applied, if not overridden.媒体查询不排除现有规则,如果没有被覆盖,它们仍然会被应用。

You can play with a demo at jsFiddle , it also shows how the orphanned textnodes are printed.您可以在 jsFiddle 玩一个演示,它还显示了孤立的文本节点是如何打印的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM