javascript - document not defined javascript error (works in Chrome console but not in integrated terminal of editor)
问题描述
I am using Visual Studio Code and I like to test my console.logs or my javascript using the integrated terminal in VSC. I have node.js installed and I use the 'node .' command to test run my javascript. I get this error
console.log(document.getElementById("para").textContent); ^
ReferenceError: document is not defined
I am just testing some stuff. My html code, javascript code
However, when I test my code through the chrome console, it works for me. No document not defined error or anything. Can anyone assist me on this? Thanks !
解决方案
document
Only exists in the browser. If you are trying to just run your Javascript directly without the accompanying browser, this will fail.
The same also goes for the window
object, these are both only available in the actual browser environment. This is the reason this works in the chrome console, as you are on a webpage and have access to these objects.
推荐阅读
- algorithm - 有健康损失的迷宫中的最短路径
- python - rpy2 和 R-imager - 如何用 Python 编写?
- c# - 没有 HTML 敏捷包的 HTMLDocument
- mysql - docker项目访问mysql容器
- amazon-s3 - 使用 cognito 托管 AWS s3 的安全性如何
- vba - 工作簿关闭时 ActiveX 复选框上的变量未定义错误
- android - MediaBrowser 未连接到 MediaBrowserService
- python - 使用 FigureCanvasTkAgg 嵌入 matplotlib 图表 - 设置 xticks
- sql - 窗口循环功能
- android - 如何停止 PagedListAdapter 初始加载动画?