【问题标题】:trying to print search "label" and "description" without printing anything else within the page尝试打印搜索“标签”和“描述”而不在页面内打印任何其他内容
【发布时间】:2016-12-29 06:10:33
【问题描述】:

我的代码有问题,但我不知道我做错了什么。 我想要完成的是,当单击“打印”按钮时,在调用 window.print() 之前,使用从“屏幕”div 绘制的内容动态填充“打印”div 我是这样向我解释的

在你的 body 标签中有两个主要的子 div。样式显示在@media 屏幕下并隐藏在@media 打印下。样式显示在@media print 下并隐藏在@media screen 下

这是我尝试的小提琴。任何帮助表示赞赏。

https://jsfiddle.net/59rphg1h/1/

        <ul>
      <li><b>No Print</b>..Don't print anything before the following search box</li>
      <li><b>No Print</b>..No graphics, Ads or anything else before the search box and its results</li>
    </ul>
      <div id="print">
      <div id="screen">

        <form class="printForm">
          <div id="printReady">
            <div class="autocompleter">
              <label> When you see a </label>
              <input type=search placeholder="&#x1f50d; Search Box Type here, Then Click Print!" name="tryit" data-using="tryit">
                <p class="content">&nbsp;</p>
              </div>
            </div>

            </form>
          </div>
        </div>
                            <input type="button" onClick="window.print();" value="Print this Recipe">
        <script>
    autocompleter.tryit = [
        {
            value: "Print this display too!",
            label: "Type then Select...,",
            desc: "<ol> <h3>When clicked print, how can I:</h3> <li> keep List for print, and eliminate everything else on the page?</li> <li> still keep the selected text (results of the search) which says --Print this display too!--</li></ol>",
        }
    ];
        </script>
        <ul>
          <li><b>No Print</b>..Don't print anything After the search box and its results</li>
          <li><b>No Print</b>..No graphics, Ads or anything else After the search box and its results</li>
        </ul>

【问题讨论】:

    标签: html css jquery-ui


    【解决方案1】:

    通过打印媒体查询打印时,您可以从页面中排除/包含特定的 HTML 元素。

    例子:

    @media print { 
       /* All your print styles go here */
       #header, #footer, #nav {
         display: none !important;
       } 
    
       ol {
         display: block !important;
       }
    }
    

    有关印刷媒体查询的更多信息,您可以阅读此article

    因此,如果您将最初需要的动态内容(ol 标记)添加到带有display: none 的页面,它将不会显示但会被打印,因为打印媒体查询将覆盖它。

    【讨论】:

    • 你在小提琴中试过了吗?我不能让它工作。看起来会更深一点,但尝试将它与 jsfiddle 一起使用
    • 创建两个新类print-onlyscreen-only。将所有内容添加到页面,为您只想在打印中显示的元素分配 print-only 类,为仅在正常视图中显示的元素分配 screen-only 类,并将以下 css 添加到您的页面:@media screen { .print-only { display: none !important; } .screen-only { display: block !important; } } @media print { .print-only { display: block !important; } .screen-only { display: none !important; } }
    • 我无法让 jsfiddle 在搜索框中做出响应(不确定那里发生了什么)。但是,我确实在其中输入了几个词,并且打印成功地删除了列表。我现在想知道这是否也会删除网页的其余部分。在我的代码中,我放置了
      作为示例,以便在测试期间删除某些内容。
        删除列表作品。我的很多问题也是网页的其余部分。
    • 关于您的问题,只需将 screen-only 类添加到您不想打印的任何 HTML 元素,并将 print-only 类添加到您不想显示的任何 HTML 元素屏幕。
    • 要消除的元素很多,例如听者、页脚、导航等,能否举个例子消除它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多