首页 > 解决方案 > 如何修复“未定义 jquery”

问题描述

我正在使用Hackernoon的教程,但出现错误:

jQuery.fn.sortDomElements = (function() {
^

ReferenceError: jQuery is not defined

在 app.js 文件中。我认为这是因为由于某种原因 app.js 和 index.html 文件没有链接在一起。在 index.html 文件中,包含了 js 文件和 jquery 脚本。(链接到 app.js 文件)

我尝试在代码中向上移动三个脚本源代码行(428-430 index.html),以确保在加载页面之前将它们包含在内,但这不起作用。

index.html 文件和 app.js 文件对我来说与示例存储库中的相同。

这是 app.js 第 8 行。

// Make a jQuery sort for the chat log based on message timetoken 
(tt)
jQuery.fn.sortDomElements = (function() {
    return function(comparator) {
        return Array.prototype.sort.call(this, 
comparator).each(function(i) {
              this.parentNode.appendChild(this);
        });
    };
})();

标签: javascriptjquerycss

解决方案


可能你丢失了一些文件,这就是它抛出错误的原因,你应该在你将使用的所有页面中包含你的 jquery

你的功能也应该是这样的

jQuery.fn.sortDomElements = (function() {

});

推荐阅读