未捕获的类型错误:$content.switchClass不是函数

Uncaught TypeError: $content.switchClass is not a function

本文关键字:switchClass content 函数 类型 错误      更新时间:2023-09-26

在JavaScript代码中显示更多显示更少返回错误。我做错了什么?

JS代码

$(".show-more a").on("click", function() {
var $this = $(this); 
var $content = $this.parent().prev("div.content");
var linkText = $this.text().toUpperCase();    
if(linkText === "SHOW MORE"){
    linkText = "Show less";
    $content.switchClass("hideContent", "showContent", 400);
} else {
    linkText = "Show more";
    $content.switchClass("showContent", "hideContent", 400);
};
$this.text(linkText);
});

控制台错误

Uncaught TypeError: $content.switchClass is not a function

包含的脚本标签

<script src="//code.jquery.com/jquery.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="js/main.js"></script>

您很可能缺少jQuery UI,因为switchClass是jQuery UI的一部分,而不是jQuery库。

正如其他人所说,您缺少jQuery UI。你的脚本标签应该是这样的:

<script src="//code.jquery.com/jquery.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="js/main.js"></script>