简体   繁体   English

如何隐藏 div 仅用于使用 js 脚本在特定链接标记上打印?

[英]How to hide div from printing only for prints over a specific link tag with js script?

The page has to have two print versions.该页面必须有两个打印版本。 One with price and on without.一个有价格,一个没有。 My solution works in Firefox, but not in chromebased Browser.我的解决方案适用于 Firefox,但不适用于基于 chrome 的浏览器。

What I do: If the printlink is klicked, I add the print:hidden-class name, then print, then remove the class:我做什么:如果打印链接被点击,我添加print:hidden-class名称,然后打印,然后删除 class:

<a href="javascript:if(window.print){document.getElementById('priceinfo').classList.add('print:hidden'); window.print(); document.getElementById('priceinfo').classList.remove('print:hidden');} ">print without price</a>

Why ist this not working in Edge or Chrome?为什么这在 Edge 或 Chrome 中不起作用? And what will work?什么会起作用?

Thanks @Fractalism moving the code into script tags worked:感谢@Fractalism 将代码移动到脚本标签中:

<script>
  document.querySelector('#print-no-price').addEventListener('click', () => {
    if(window.print) {
      document.getElementById('priceinfo').classList.add('print:hidden'); 
      window.print(); 
      document.getElementById('priceinfo').classList.remove('print:hidden');
    }
  });
</script>

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

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