[英]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')
In my app.js file, I am manipulating a lot of elements, which is coming from an index.html file, connected to the said app.js file as a script.在我的 app.js 文件中,我正在操作很多元素,这些元素来自 index.html 文件,作为脚本连接到上述 app.js 文件。
Is there any way I can accomplish what I'm trying to do here, but on the node.js?有什么方法可以完成我在这里尝试做的事情,但是在 node.js 上? I just need to add a database so I can save highscores and player names.
我只需要添加一个数据库,这样我就可以保存高分和玩家姓名。
Thank you.谢谢你。
I was able to bypass nmy problem by just linking my DOM manipulation from a seprate index.js file, instead of manipulating from my app.js which runs my node.通过将我的 DOM 操作与单独的 index.js 文件链接起来,我能够绕过 nmy 问题,而不是从运行我的节点的 app.js 进行操作。 Thank you for your help guys.
谢谢你们的帮助。
Nodejs is server-side. Nodejs 是服务器端的。 It really isn't supposed to use methods from
windows
or它真的不应该使用
windows
或
document
. document
。 If you do it will say it's undefined.如果你这样做,它会说它是未定义的。 There is one way and that is to use puppeteer.
有一种方法,那就是使用 puppeteer。
https://www.npmjs.com/package/puppeteer https://www.npmjs.com/package/puppeteer
Keep in mind you must run puppeteer commands asynchronously.请记住,您必须异步运行 puppeteer 命令。 But for the basics and to just do what you want to do try:
但是对于基础知识并只是做你想做的事,试试:
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();
}
Check out the puppeteer documentation here: https://pptr.dev/在此处查看 puppeteer 文档: https://pptr.dev/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.