javascript - 未捕获的类型错误:$(...).makeRed 不是函数
问题描述
我想用 jquery 选择器调用自定义 javascript 函数,但这说 Uncaught TypeError: $(...).makeRed is not a function
<!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>
推荐阅读
- python - AttributeError:“NoneType”对象没有属性“预测”
- r - 嵌套最大化与在 R 中使用全局变量的需要并行
- generics - Kotlin 泛型不能使用 T 作为具体类型参数
- graph - 将文件层次结构制作为边列表,然后将树结构绘制为图形
- node.js - 如何在flutter(解密)和Node(加密)中实现RSA?
- django - 如何创建所有 Django 项目详细信息页面 url 的 xml 站点地图
- python - macOS python 2.7错误:IndexError:列表索引超出范围
- spring - Redis Sentinel 认证使用 Lettuce+SpringDataRedis
- selenium - 下拉列表/Xpath 帮助(Selenium w/Python
- python - 与值表迭代合并 - 以区间之间的值为条件(熊猫)