简体   繁体   English

根据单击的元素打印页面的不同部分

[英]Printing different part of page depending on clicked element

I'm looking for a clever way to print a small part of a page, depending on what link the user is clicking. 我正在寻找一种聪明的方法来打印页面的一小部分,具体取决于用户单击的链接。 An example: 一个例子:

Item 1  PRINT
Item 2  PRINT

If I click on print next to Item 1, I just want Item 1 to open up in print dialog. 如果单击项目1旁边的打印,我只希望在打印对话框中打开项目1。 Likewise I only want Item 2 to open up if I click next to that item. 同样,如果我单击项目2旁边的,则只希望它打开。

I know of <a href="javascript:window.print()"> and how to add for instance a class named noprint to whatever element I would like not to be printed, but what I'm looking for is some clever, dynamic way of having it change depending on what element on the page the user is interacting with. 我知道<a href="javascript:window.print()">以及如何将名为noprint的类添加到我不想打印的任何元素上,但是我正在寻找的是一个聪明,动态的东西根据用户与页面交互的元素而改变它的方式。

Anyone know of a way to solve this? 有人知道解决此问题的方法吗?

This is my favorite way of doing it. 这是我最喜欢的方式。 It doesn't work here on S/O, but it works in this pen 这在S / O上不起作用,但在此笔中有效

  function printDiv(id) { var printContents = document.getElementById(id).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } 
 <div id="first">I'm first</div> <button onclick="printDiv('first')">Print</button> <div id="second">I'm second</div> <button onclick="printDiv('second')">Print</button> 

  1. Create separate action on backend which will render desired item in appropriate ready to print view (somthing like /print?itemId=123 ) 在后端创建单独的操作,该操作将在适当的就绪打印视图中呈现所需的项目(类似/print?itemId=123
  2. By click on link open that view in separate window and there call window.print(); 通过单击链接在单独的窗口中打开该视图,然后调用window.print();

It seems like a big job for small task, but you'll get significant benefit of independent and flexible on-screen and print views of same item. 对于小任务来说,这似乎是一项艰巨的工作,但是独立和灵活的屏幕上和同一项目的打印视图将为您带来巨大的好处。

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

相关问题 html不同的页面内容,取决于单击的内容 - html different page content depending on what was clicked 根据单击的内容,对不同的元素执行相同的功能-聚合物 - Perform same function on different element depending on which is clicked - Polymer 元素的高度取决于页面加载 - Different height of element depending on page load 滚动到页面上的相同位置,具体取决于单击哪个元素 - Scroll To same position on page depending on which element is clicked 如何根据用户单击的按钮打开不同样式的页面? - How to open page with different style depending on which button was clicked by the user? 单击链接到同一页面的不同部分时,禁用URL更改 - disable url change when link to a different part of the same page is clicked 滚动到页面,具体取决于单击的按钮 - Scroll to page depending on button that is clicked 如何在网页上有浮动/固定元素,具体取决于页面的哪一部分可见? - How do you have a floating/fixed element on a web page depending on which part of the page is visible? 是否可以链接到具有不同 CSS 条件的同一页面,具体取决于我单击的链接? - Is it possible to link to the same page with different CSS conditions active, depending on which Link I clicked on? 如何根据单击的元素更改变量 - How to change variable depending on element clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM