Uncaught ReferenceError: $ is not a function
在这篇文章中,我们将看到 “未发现的引用错误:不是一个函数”。ReferenceError是一个代表错误的对象,当变量没有被初始化或者简单地说,变量不存在于当前的范围内而被引用时,就会产生错误。符号是jQuery的一个别名。没有被定义的ReferenceError通常出现在jQuery没有被加载,而JavaScript没有识别到符号的时候。
要解决这个错误,首先,在标题部分使用jQuery CDN链接,或者下载jQuery文件,并在标题部分使用jQuery文件链接。
例子:这个例子创建了一个ReferenceError,因为jQuery没有被加载,而且JavaScript不能识别$符号。
<!DOCTYPE html>
<html>
<head>
<script>
(document).ready(function() {
("button").click(function() {
("p").hide();
});
});
</script>
</head>
<body>
<h2>GeeksforGeeks</h2>
<p>This example shows when the is not defined.</p>
<button>Hide Content</button>
</body>
</html>
输出:
为了解决这个错误,我们只需在标题部分添加jQuery CDN链接或下载的jQuery路径链接。
jQuery CDN链接:
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script>
例子:这个例子是通过在 “我的 “<script>
标签中添加所需的CDN链接来解决错误。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
(document).ready(function() {
("button").click(function() {
("p").hide();
});
});
</script>
</head>
<body>
<h2>GeeksforGeeks</h2>
<p>
This example shows when the is not
defined error arises due to the required
CDN link being defined
</p>
<p>Click the button to see the change</p>
<button>Hide Content</button>
</body>
</html>
输出: