【问题标题】:NodeJS 'jquery' npm package not working($.on())NodeJS 'jquery' npm 包不工作($.on())
【发布时间】:2015-12-12 01:33:41
【问题描述】:

我已经安装并加载了jquery $ = require('jquery');

但是当我尝试使用它时:

app.get('/', function (req, res) {
    res.render('index');
    $.on('ready', function () {

    }
}

它给我一个TypeError: $.on is not a function

有人告诉我使用“jQuery”,但不推荐使用“jQuery”,它告诉我使用“jquery”

编辑:同样废弃的 jQuery 包甚至无法正确加载

【问题讨论】:

  • 在这种情况下 $.on 应该做什么?
  • 就在错误中,$.on 不是函数。
  • 尽量不要在服务器上使用jQuery。

标签: javascript jquery node.js npm


【解决方案1】:

我找到了一种方法,可以直接在页面上加载 jQuery 包,并允许它在 NodeJS 上使用。

这是一个使用 JADE 和 EXPRESS 完成的示例。

首先你加载 jQuery 包 script(src='/js/jQuery.js') 然后在你有 jQuery 包 res.render('index'); 的地方渲染你的索引,现在你可以在 NodeJS 上使用 jQuery

不要忘记 而不是使用document window 你必须使用response.window response.document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 2017-05-04
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    相关资源
    最近更新 更多