jQuery“.cookie"美元;不是函数

jQuery "$.cookie" not a function

本文关键字:函数 美元 quot cookie jQuery      更新时间:2023-09-26

我不知道为什么我在控制台中得到这个简单的错误:

Uncaught TypeError: $.cookie is not a function

这里是我实现JavaScript和jQuery的地方:

<script src="jquery.js"></script>
<script src="cookies.js"></script>
<script src="scripts/changeTheme.js"></script>
<script>
    function cookieTest() {
        $.cookie("test", "Cookies are working!");
        alert($.cookie("test"));
    };
    cookieTest();
</script>

我检查了一千遍,我没有看到一个参考打字错误。我试着离线和在线浏览我的网站。

我从https://github.com/js-cookie/js-cookie下载了cookies插件。

同样,当我这样做时,警报也会起作用:

<script src="jquery.js"></script>
<script src="cookies.js"></script>
<script src="scripts/changeTheme.js"></script>
<script>
    function cookieTest() {
        alert("Test");
    };
    cookieTest();
</script>

我的文件被命名为cookies.js,因为我只是从github复制了源代码,并把它放在一个新的js文件中。目录

您使用的库不是jquery插件。使用旧版本-https://github.com/carhartl/jquery-cookie/blob/master/src/jquery.cookie.js

因为我没有找到这个插件的任何cdn源,我复制粘贴了它的内容,它正在工作。例如:https://jsfiddle.net/wbcq3ho4/

<script src="CORRECT LIBRARY PATH HERE"></script>
<script>
    function cookieTest() {
        $.cookie("test", "Cookies are working!");
        alert($.cookie("test"));
    };
</script>
<button type="button" onclick="cookieTest();">
Click me
</button>

只需手动下载文件或使用wer/composer,并将正确的链接放在你的html代码

这个库不是基于jQuery的,请确保您检查了如何在GitHub上使用它

Cookies("yourCookie","yourCookieText");