简体   繁体   English

Javascript按钮滚动顶部并打印

[英]Javascript button scroll top and print

I need to create a button whose function is to scroll to the top of the page and then print it.我需要创建一个按钮,其功能是滚动到页面顶部然后打印。

My code attached to the button is the following:我附加到按钮的代码如下:

<script>
jQuery(document).ready(
    function($){
        $('#backToTop').click(function(){
            $("html, body").animate({ scrollTop: 0 }, 500, function() {             
                window.print();
            });
        });
    }
);
</script>

And the button还有按钮

<a id="backToTop">Print</a>

but the print method runs twice.但打印方法运行两次。 I can't find a solution to prevent printing from running twice.我找不到防止打印运行两次的解决方案。

How can this be fixed?如何解决这个问题?

The event will fire twice because of your selector html, body .由于您的选择器html, body该事件将触发两次。

What is event bubbling and capturing? 什么是事件冒泡和捕获? this might come in handy.这可能会派上用场。

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

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