javascript - 打印带有已单击的 div 的 HTML 页面并隐藏其余部分

标签 javascript html printing

我有一个 HTML 页面,其中有很多不同的 DIV,我想打印出用户单击的 DIV 并隐藏所有其余部分。

有人可以告诉我如何用 JavaScript 做到这一点吗?

<html>
  <head>
    <title>Print Demo</title>

    <script type="text/javascript">
       <!-- MY JAVASCRIPT FUNCITON -->

    </script>

  </head>
  <body>

    <div id="div1">
       <a href="<JSMethod>">Print the page with this div</a>
    </div>

    <div id="div2">
       <a href="<JSMethod>">Print the page with this div</a>
    </div>

    <div id="div3">
       <a href="<JSMethod>">Print the page with this div</a>
    </div>

    </body>
</html>

最佳答案

这只是为了扩展pimvdb's answer .

jQuery:

$("a").on("click", function(){
    $("div").hide();
    $(this).parent().show();
});

或者按照建议:

$("a").on("click", function(){
    $("div").hide();
    $(this).closest("div").show();
});

关于javascript - 打印带有已单击的 div 的 HTML 页面并隐藏其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10380605/

相关文章:

javascript - 不显示 PHP 表单操作页面

Javascript AOP 支持

javascript - 如何组织两个 div 之间的交互?

php - 突出显示存档中的类别 - Wordpress

html - 在 div 中堆叠元素时遇到问题

html - 网页到打印媒体——解决方案?

javascript - React Native ScrollView/ListView RefreshControl 最初在右上角很难看

javascript - WebStorm + Nodemon 中的远程调试问题

c++ - Windows 上的 QPrintDialog::exec() 错误

linux - 使用 Cups 打印多个 Dymo 标签