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

问题描述 投票:0回答:1

我想用jquery选择器调用自定义javascript函数,但这说未捕获的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 exception client-side
1个回答
0
投票

您在就绪状态上创建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>
© www.soinside.com 2019 - 2024. All rights reserved.