【问题标题】:Document is not defined Javascript文档未定义 Javascript
【发布时间】:2020-04-18 14:44:49
【问题描述】:

我在观看视频并准确复制代码时不断收到此错误。

var scores, roundScore, activePlayer, dice

scores = [0,0]
roundScore = 0
activePlayer = 0

dice = console.log (Math.floor(Math.random() * (6) + 1))
console.log(dice)

document.querySelector('#current-0').textContent = dice
ReferenceError: document is not defined
    at init (/Users/Nikolas/Desktop/JS/app.js:79:5)
    at Object.<anonymous> (/Users/Nikolas/Desktop/JS/app.js:4:1)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

【问题讨论】:

  • 您是否尝试在 Node.js 中打开 .js 文件?
  • 您收到此错误是因为您在 DOM(documentwindow 等)所在的 服务器端节点环境中运行 JavaScript t 可用 - 这就是您收到错误的原因。 (即 NodeJS 不应运行 app.js)。我不知道为什么 NodeJS 正在运行 app.js - 请发布你用来启动 web 服务器和任何 bower、webpack 或 gulp 任务的命令行。

标签: javascript document


【解决方案1】:

$(document).ready(function() {
    $(function () {
      //Do my stuff
    });
});

而不是上面的代码调用这个

<script>
    $(document).ready(function() {
      //call your script.js function from here
    });
</script>

【讨论】:

  • document 必须首先定义(即使您的代码使用它:$(document)
  • 最初如何定义?
  • 我对这个答案投了反对票,因为 OP 没有使用 jQuery(而且我们不应该再使用 jQuery)。
猜你喜欢
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2016-05-06
  • 2020-10-26
  • 2015-11-14
相关资源
最近更新 更多