[英]How do I manipulate the DOM using node.js? I have done my document.get only to find out node.js doesn't recognize it
const express = require('express'),
app = express(),
path = require('path'),
mongoose = require('mongoose'),
userGuess = document.getElementById('userGuess'),
lastResult = document.getElementById('lastResult'),
numRound = document.getElementById('numRound'),
roundDiv = document.getElementById('roundDiv'),
correctNum = document.getElementById('correctNumber'),
startButton = document.getElementById('startButton'),
gameInputs = document.getElementById('gameInputs'),
guessSubmit = document.getElementById('submitGuess'),
health = document.getElementById('health'),
easy = document.getElementById('easy'),
normal = document.getElementById('normal'),
hard = document.getElementById('hard'),
labelDifficulty = document.getElementById('labelNum'),
points = document.getElementById('points'),
span = document.getElementsByClassName('close')[0],
modal = document.getElementById('myModal'),
playerNames = document.getElementById('playerNames'),
playerName = document.getElementById('playerName'),
submitName = document.getElementById('submitName'),
cheat = document.getElementById('cheatMod'),
resetGame = document.getElementById('resetGame')
在我的 app.js 文件中,我正在操作很多元素,這些元素來自 index.html 文件,作為腳本連接到上述 app.js 文件。
有什么方法可以完成我在這里嘗試做的事情,但是在 node.js 上? 我只需要添加一個數據庫,這樣我就可以保存高分和玩家姓名。
謝謝你。
通過將我的 DOM 操作與單獨的 index.js 文件鏈接起來,我能夠繞過 nmy 問題,而不是從運行我的節點的 app.js 進行操作。 謝謝你們的幫助。
Nodejs 是服務器端的。 它真的不應該使用windows
或
document
。 如果你這樣做,它會說它是未定義的。 有一種方法,那就是使用 puppeteer。
https://www.npmjs.com/package/puppeteer
請記住,您必須異步運行 puppeteer 命令。 但是對於基礎知識並只是做你想做的事,試試:
const puppeteer = require('puppeteer');
var x = async(){
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('<url>');
await page.evaluate(() => {
//insert code here like you would in the console in your browser's devTools
})
await browser.close();
}
在此處查看 puppeteer 文檔: https://pptr.dev/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.