javascript 参考错误:创建变量时未定义文档

dced5bon  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(113)

我在node.js中运行我的代码,我已经看到在node中运行代码可能会起作用。但这从来不是问题。它一直说我的变量指向我的元素"document is not defined"。我不知道我做错了什么,我正确地链接了它,但仍然感到困惑。我还尝试用defer将我的脚本放在底部和顶部。

let seconds = 00;
let tens = 00;
let appendTens = document.querySelector('#tens');
let appendSeconds = document.querySelector('#seconds');
let stop = document.querySelector('#button-stop');
let start = document.querySelector('#button-start');
let reset = document.querySelector('#button-reset');
let interval; //store timer values

// this function will run when start is clicked 

const startTimer = () =>{
    tens++

    if(tens < 9){
        appendTens.textContent = `0${tens}`
    }
    if(tens > 9){
        appendTens.textContent = tens;
    }
    if(tens > 99){
        seconds++
        appendSeconds.textContent = `0${seconds}`;
        tens = 0;
        appendTens.textContent = "0" + 0;
    }
    if(seconds > 9){
        appendSeconds.textContent = seconds;
    }
};

start.onclick = function(){
    interval = setinterval(startTimer)
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stopwatch</title>
    <link rel="stylesheet" href="stopwatch.css">
</head>
<body>
    <div class="wrapper">
        <h1>Stopwatch</h1>
        <h2>Vanilla JavaScript Stopwatch</h2>
        <p><span id="seconds">00</span>:<span id="tens">00</span></p>
        <button id="button-start">Start</button>
        <button id="button-stop">Stop</button>
        <button id="button-reset">Reset</button>
        </div> 
        <script type="module" src="stopwatcj.js"></script>
</body>
</html>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-self: center;
  background-color: rgb(248, 180, 55);
}

.wrapper {
  margin-top: 10%;
  text-align: center;
  color: #fff;
}
.wrapper button {
  background-color: rgb(248, 180, 55);
  border: 1px solid white;
  padding: 10px 20px;
  color: #fff;
}
.wrapper button:hover {
  cursor: pointer;
  opacity: 30%;
}
cdmah0mi

cdmah0mi1#

作为Html DOM一部分的文档不是nodejs的一部分。由于您可能使用nodejs来编译您的js代码,这就是您收到此错误的原因。请尝试简单地在浏览器中运行此错误。

相关问题