简体   繁体   English

为什么在我的 JS 文件中编写 JS 代码时,我的 JS onscroll 功能不起作用?

[英]Why does my JS onscroll function dont work when I write the JS code in my JS file?

My JS onclick function work when I put the code in script tags in the html file under the div.当我将代码放在 div 下的 html 文件中的脚本标签中时,我的 JS onclick 函数工作。 But I want to put the script in to the JS file and this dont work.但是我想将脚本放入 JS 文件中,这不起作用。

HTML: HTML:

   <script type="text/javascript" src="js/javascript.js"></script>

   <div class="col-sm-6" id="test" onscroll="scrollback()">
        <?php  $controller->getEvents(); ?>
   </div>

JS (js/javascript.js): JS(js/javascript.js):

   $(document).ready(function () {

   var isCounting = false;

    function scrollback() {
        if (isCounting == false) {
            isCounting = true;
            setTimeout(
                function () {
                    document.getElementById("test").scrollTo({
                        top: 0,
                        left: 0
                    });
                    isCounting = false;
                }, 1000);
        }
    }

    }

Try placing the script tag at last in body tag.尝试将 script 标签最后放在 body 标签中。 This ensures the HTML elements loads first then the Js file loads.这确保首先加载 HTML 元素,然后加载 Js 文件。

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

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