简体   繁体   English

在javascript中初始化datepicker时,为什么html页面向上滚动?

[英]When the datepicker is initialized in javascript, why is the html page scrolled up?

I have an HTML page, laravel blade exactly.我有一个 HTML 页面,正好是 laravel 刀片。 My question is when the date picker is initialized in javascript, why is the HTML page scrolled up?我的问题是当日期选择器在 javascript 中初始化时,为什么 HTML 页面向上滚动? This is the code at the bottom of the page:这是页面底部的代码:

<script type="text/javascript">
        $(document).ready(function () {            
            $('.date').datepicker({
                firstDayOfWeek: 1,
                weekDayFormat: 'narrow',
                inputFormat: 'd/M/y',
                outputFormat: 'd/M/y',
                markup: 'bootstrap4',
                theme: 'bootstrap',
                modal: false
            });
        });
    </script>

I tried to remove the javascript code and I ran it from the Chrome inspector, in the console tab after the page was ready.我试图删除 javascript 代码,并在页面准备好后在控制台选项卡中从 Chrome 检查器运行它。 The page goes to the top.页面转到顶部。

Why?为什么? Is it possible to avoid this behavior?是否可以避免这种行为?

The code you've included isn't causing the page to scroll.您包含的代码不会导致页面滚动。 You've likely got one of the inputs focused when you run it, or there is some other javascript which is interacting with the code from your question.当您运行它时,您可能已经关注了其中一个输入,或者还有其他一些 javascript 正在与您的问题中的代码进行交互。

 window.scrollTo(0,document.body.scrollHeight); // scroll to bottom $(document).ready(function () { $('.date').datepicker({ firstDayOfWeek: 1, weekDayFormat: 'narrow', inputFormat: 'd/M/y', outputFormat: 'd/M/y', markup: 'bootstrap4', theme: 'bootstrap', modal: false }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <input type="date" class="date"> <div style="height: 200vh"></div>

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

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