未捕获的 TypeError:$(...)。makeRed 不是一个函数

Uncaught TypeError: $(...).makeRed is not a function

提问人:farhad shah 提问时间:4/24/2020 更新时间:4/24/2020 访问量:454

问:

我想使用 jquery 选择器调用自定义 javascript 函数,但这说 Uncaught TypeError: $(...)。makeRed 不是一个函数

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<h3 id="ch">Hello Wrld</h3>

</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
    $(document).ready(function(){
        $.fn.makeRed = function(){
            this.html('welcome to all');
            return this;
    }
});
    $('#ch').makeRed();


</script>
JavaScript jQuery jQuery-UI 异常 客户端

评论

1赞 Teemu 4/24/2020
把线也放进去。$('#ch').makeRed();$(document).ready(function(){...})

答:

0赞 Joel Hager 4/24/2020 #1

在就绪状态下创建 makeRed 之前,您正在调用它。把它放在 这里是固定的片段。:)});

    $(document).ready(function(){
        $.fn.makeRed = function(){
            this.html('welcome to all');
            return this;
    }
     $('#ch').makeRed();
});
   
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<h3 id="ch">Hello Wrld</h3>

</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>