首页 > 解决方案 > Uncaught TypeError: $(...) is not a function - 当自调用函数位于脚本底部时

问题描述

代码片段是较大代码的可重现部分。在这里,如果我在底部编写自调用函数,它会给我“Uncaught TypeError: $(...) is not a function”,但如果我将它移到顶部,它会起作用。谁能帮我理解为什么会出现这个错误。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Learning</title>
    <link rel="stylesheet" href="./style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script>

        $(function() {
            $(window).scroll(function(){
                var height = $(window).height();
                var scrollTop = $(window).scrollTop();
                $('.status').html(scrollTop)
            });
        });

        (function(a){
                console.log(a);
        }("sometext"));
     
             
    </script>
 
</head>
  <body>
    <div class="status"></div>
  </body>
</html>

标签: jquery

解决方案


推荐阅读