首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用javascript打印页面的原件、复印件和三份复印件?

如何使用javascript打印页面的原件、复印件和三份复印件?
EN

Stack Overflow用户
提问于 2017-08-11 15:41:42
回答 2查看 2.6K关注 0票数 0

我有一个示例html页面,我想打印这个页面只有三个副本,也改变原始(在右上角提到),只有当我们采取这个页面的第二个副本复制。我们有没有办法使用Javascript或Jquery来实现这一点呢?

代码语言:javascript
运行
复制
function myFunction() {
    window.print();
}
代码语言:javascript
运行
复制
 .original  {
float : right;
margin-right : -0.1cm;
}
代码语言:javascript
运行
复制
<p>Click the button to print the current page.</p>

<button onclick="myFunction()">Print this page</button>

<div class="col-xs-4"></div>
       <div class="col-xs-1 original"><font color="red"><strong>Original</strong></font></div>

EN

回答 2

Stack Overflow用户

发布于 2017-08-11 16:04:21

我试着给出一个简单的解决方案,暂时保留打印计数的标志。请查看下面的内容。

代码语言:javascript
运行
复制
var v1=0;
function myFunction() {
   if (v1==0) {
  window.print();
   }
   else {
  window.print();
  $('#s1').text('Duplicate');
   }
   v1++;
}
代码语言:javascript
运行
复制
.original  {
float : right;
margin-right : -0.1cm;
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click the button to print the current page.</p>

<button onclick="myFunction()">Print this page</button>

<div class="col-xs-4"></div>
       <div class="col-xs-1 original"><font color="red"><strong id="s1" >Original</strong></font></div>

希望这能对你有所帮助!

票数 0
EN

Stack Overflow用户

发布于 2019-08-23 13:27:28

这里是示例代码。现在请查收。

代码语言:javascript
运行
复制
    #printable { display: none; }

    @media print
    {
        #non-printable { display: none; }
        #printable { display: block; }
    }
    </style>
</head>
<body>
    <div id="non-printable">
        Your normal page contents
    </div>
    <div id="printable">
        Printer version <div id="count"></div>
    </div>
    <a href="#" id="autoclick" onclick="window.print();">Print<a>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
(function() {

    var beforePrint = function() {
        console.log('Functionality to run before printing.');
    };

    var afterPrint = function() {
        console.log('Functionality to run after printing');
          var vals = $('#inc').val();
          var incval = parseInt(vals)+parseInt(1);
           $('#inc').val(incval);
           if(incval < 3)
           {   

              window.setTimeout('window.print()',5000); 
           }
    };

    if (window.matchMedia) {
        var mediaQueryList = window.matchMedia('print');
        mediaQueryList.addListener(function(mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

    window.onbeforeprint = beforePrint;
    window.onafterprint = afterPrint;

}());
</script>
<input type="text" id="inc" value="0">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45629602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档