【问题标题】:How to make jQuery 3.6.0 work with node.js v12.16.1如何使 jQuery 3.6.0 与 node.js v12.16.1 一起工作
【发布时间】:2021-09-20 10:51:11
【问题描述】:

我正在使用一个名为 Replit 的在线 IDE,并且 node.js 项目在节点版本上运行:12.16.1。但我试图让 jQuery 3.6.0 与这个版本的 node.js 一起工作,但是我在网上找到的每一种方法都不起作用。虽然我没有使用 html,但Pug 是一种在 html 文件中插入变量的方法。

我尝试使用的第一种方法是使用 cdn 导入 jQuery:

script(src='https://code.jquery.com/jquery-3.6.0.slim.min.js', integrity='sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=', crossorigin='anonymous')

第二种方法是使用jQuery npm包:

const { JSDOM } = require( 'jsdom' );
const jsdom = new JSDOM();
const { window } = jsdom;
const { document } = window;
global.window = window;
global.document = document;

const $ = global.jQuery = require( 'jquery' );
console.log( `jQuery ${jQuery.fn.jquery} working! Yay!!!` );

console.log() 确实有效,但代码运行不正确。

代码如下:

const { JSDOM } = require( 'jsdom' );
const jsdom = new JSDOM();
const { window } = jsdom;
const { document } = window;
global.window = window;
global.document = document;

const $ = global.jQuery = require( 'jquery' );
console.log( `jQuery ${jQuery.fn.jquery} working! Yay!!!` );

const users = require('../../../data/user')
const sessions = require('../../../data/session')

let session = {}
let user = {}

$('#createSessionButton').on("click", async () => {
  session = await sessions.create({
    name: $('#session').val(),
    players: $('#players').val(),
    rewardMultiplier: $('#multiplier').val()
  })

  user = await users.create({
    session: session.name,
    username: $('#username').val(),
    email: $('#email').val(),
    password: $('#password').val(),
    isAdmin: true,
    total: 0
  })
})

$('#joinSessionButton').on("click", async () => {
  session = await sessions.findOne({ name: $('#session').val() })

  user = await users.findOne({
    session: session.name,
    username: $('#username').val(),
    email: $('#email').val(),
    password: $('#password').val()
  }) || await users.create({
    session: session.name,
    username: $('#username').val(),
    email: $('#email').val(),
    password: $('#password').val(),
    isAdmin: false,
    total: 0
  })
})

module.exports = session, user

基本上,一旦按下按钮,就会获取多个输入 html 标记的文本值,使用它们的 id(在 pug 中,您使用 # 来声明 id => 示例:h3#test Test == Test),发送到数据库。

这是使用此脚本的 pug/jade 文件之一:

doctype
html(lang="en")
  head
    include ../includes/header.pug
    link(rel="stylesheet", href="/css/session.css")

  body
    h1.mb-3.font-weight-normal(style="margin-top: 7.5vw; margin-bottom: 5vw;") Please fill in each and everyone of these fields to create a session
      form(style="max-width: 480px; margin: auto;")

        h2.mt-5 Session Information
        h5 (This will create a session for you and your friends to play together)
        input#session.form-control.mb-3(type="text", placeholder="Session Name", required)
        input#players.form-control.mb-2(type="number", min='5', max='25', placeholder="Number of Players", required)
        input#multiplier.form-range(type='range', value="1", min='0.5', max='10', step='0.5', onchange="rangevalue.value = `Reward Multiplier : ${value}`")
        output#rangevalue.h4 Reward Multiplier : 1

        h2.mt-5 User Information
        h5 (This will create or fetch your account)
        input#username.form-control.mb-3(type="text", placeholder="Username", required)
        input#email.form-control.mb-3(type="email", placeholder="Email Address", required)
        input#password.form-control(type="password", placeholder="Password", required)

      div.mt-3.center
        a.btn.btn-lg.btn-primary(href='/session') <-- Back
        a#createSessionButton.btn.btn-lg.btn-primary.btn-block(href="/session/dashboard") Create session -->
        
    script(src="/js/session.js")

但是 jQuery 在我的项目中根本不起作用,即使它在 Web 浏览器控制台中起作用。如果有人可以帮助我,那就太棒了!!!

这里是网络浏览器的链接:Website

【问题讨论】:

  • 所以... Node.js 是服务器端 JavaScript 运行时环境,而不是 Web 浏览器。它没有用户界面,因此人类无法点击任何东西。 Pug 是一种生成 HTML 的模板语言,当由 Node 提供服务时,Web 浏览器将使用它来呈现人类可以单击的用户界面。在服务器端编写 jQuery 是没有意义的,因为点击发生在客户端,在浏览器上,服务器对它们一无所知。我认为你需要做更多的研究。

标签: javascript jquery node.js npm cdn


【解决方案1】:

@HereticMonkey 说的是真的。您问题顶部的script() 行是正确的方法,因为它将jquery 加载到您的网页,而不是您的服务器。它从 CDN 加载它。这很好。

jquery 使用浏览器端(客户端)javascript 来操作网页中内容的文档对象模型 (DOM)。

你的 nodejs 服务器中没有 DOM(除非你正在做一些非凡的事情——你不是)所以服务器中的 jquery 毫无意义。

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多