[英]How can I solve document is not defined error?
I am getting this error I thought I would be because of the node.js but I am not sure of it.我收到这个错误,我以为我会是因为 node.js,但我不确定。 How can I solve it ?
我该如何解决?
[Running] node "c:\Users\Lenovo\Desktop\projectjs\index.js"
c:\Users\Lenovo\Desktop\projectjs\index.js:1
var counter = document.getElementById("count-el")
^
ReferenceError: document is not defined
at Object.<anonymous> (c:\Users\Lenovo\Desktop\projectjs\index.js:1:15)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
var counter = document.getElementById("count-el") var count = 0 function increment() { count = count + 1 counter.innerText = count }
<body> <div> <h1>People entered:</h1> <h2 id="count-el">0</h2> <button id="increment-btn" onclick="increment">INCREMENT</button> </div> <script src="index.js"></script> </body>
Your JavaScript is designed to be embedded in an HTML document, loaded by a <script>
element and executed by a web browser.您的 JavaScript 旨在嵌入 HTML 文档中,由
<script>
元素加载并由 Web 浏览器执行。
It is not designed to run in Node.js which isn't centred around documents at all.它不是为在根本不以文档为中心的 Node.js 中运行而设计的。
Open the HTML document in a web browser.在 Web 浏览器中打开 HTML 文档。 It already has a
<script>
element.它已经有一个
<script>
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.