我想用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>
您在就绪状态上创建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>