javascript - 在 `*` 中使用 CSS `:not()` 和 `@media print` 仍然可以显示所有内容。是什么原因?

标签 javascript css

尝试选择将在 window.print() 上打印的内容。

添加了最外层元素,因此它包含子元素(解释使用.printable .printable * 的原因),class使用 printable,并添加样式:

@media print{
    *:not(.printable *, .printable){
        display: none !important;
    }
}

调用 window.print() 时,仍然会显示所有内容。

Example

编辑 1

尝试使用 jQuery 将 notPrintable 添加到所有 :not(.printable, .printable *, .notPrintable) 并引用 notPrintable @media print

代码:

jQuery:

$(":not(.printable, .printable *, .notPrintable)").addClass("notPrintable");

CSS:

@media print{
    .notPrintable{
        display: none !important;
    }
}

现在没有显示。

Example 2

编辑 2

如果有人需要,请将解决方案示例放在这里(只有在将答案标记为已接受后才可用):

Example Final

最佳答案

您可以隐藏所有元素并覆盖打印媒体查询中的特定类。

@media print{
  body *:not(.printable) {
    display: none;
  }
}

关于javascript - 在 `*` 中使用 CSS `:not()` 和 `@media print` 仍然可以显示所有内容。是什么原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165412/

相关文章:

HTML & CSS : ">" Greater than selector styles not applied

javascript - 在 acrobat PDF 中分配 mouseUp 事件

javascript - 转换时间 hh :mm:ss into GMT time format in Angular 2

javascript - 在多次打开的列表之间移动

javascript - 列出 CSS 自定义属性(CSS 变量)

javascript - 使用 JavaScript 覆盖/删除 CSS 属性

css - 当客户端是 IE8 时,GWT 不应用 Css 样式

javascript - 在 React-Slick 上修改 CSS

javascript - 如何将隐藏信息发送到打印机进行打印

javascript - 无法让此 CSS 过渡在 iPhone 上正常运行