【问题标题】:Division in JavaScript giving undefined errorJavaScript中的除法给出未定义的错误
【发布时间】:2021-04-27 18:52:57
【问题描述】:

我正在尝试计算用户开始输入和结束输入之间的时间。所以我用Date.now(); 函数命名了两个常量。每当我运行程序时,它总是给出错误:ReferenceError: start is not defined at HTMLInputElement.element.onkeyup (/main.js:86:16) 代码如下:

Javascript:

var input = document.getElementById("boch"); input.addEventListener("keyup", function (event) { if (event.keyCode === 13) { event.preventDefault(); document.getElementById("bocho").click(); } }); var element = document.querySelector("#boch"); element.onkeyup = function () { 变量值 = 元素值; if (value.includes("m")) { 让开始 = Date.now(); } if (value.includes("man")) { document.getElementById('word-1').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-1').style.backgroundColor = "red"; } if (value.includes("man become")) { document.getElementById('word-2').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-2').style.backgroundColor = "red"; } if (value.includes("man become as")) { document.getElementById('word-3').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-3').style.backgroundColor = "red"; } if (value.includes("man become as and")) { document.getElementById('word-4').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-4').style.backgroundColor = "red"; } if (value.includes("man become as and through")) { document.getElementById('word-5').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-5').style.backgroundColor = "red"; } if (value.includes("man become as and through find")) { document.getElementById('word-6').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-6').style.backgroundColor = "red"; } if (value.includes("man become as and through find would")) { document.getElementById('word-7').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-7').style.backgroundColor = "red"; } if (value.includes("man become as and through find will here")) { document.getElementById('word-8').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-8').style.backgroundColor = "red"; } if (value.includes("man become as and through find will here and")) { document.getElementById('word-9').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-9').style.backgroundColor = "red"; } if (value.includes("man 通过 find will here 和 before")) { document.getElementById('word-10').style.backgroundColor = 'green'; } 别的 { document.getElementById('word-10').style.backgroundColor = "red"; } if (value.includes("man 通过 find will here 和 before")) { 让结束 = Date.now(); } 让millis =开始/结束; console.log(`经过的秒数 = ${Math.floor(millis / 1000)}`); }

HTML:

<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<h1>
   <span id="word-1">man</span> <span id="word-2">become</span> <span id="word-3">as</span>
   <span id="word-4">and</span> <span id="word-5">through</span> <span id="word-6">find</span> <span id="word-7">would</span> <span id="word-8">here</span> <span id="word-9">and</span> <span id="word-10">before</span>
</h1>

<input type="text" id="boch" autocomplete="off">

        </div>
        <div id="typing-area">

      <button id="bocho" onclick="document.getElementById('boch').value = ''">Enter</button>

</html>




<script src="main.js"></script>

【问题讨论】:

标签: javascript html css date


【解决方案1】:

您声明变量“start”的位置与使用时不同。这意味着

let millis = start/end;

无法看到开始。

您需要将 let 更改为 var。这样范围就不会以同样的方式受到限制

【讨论】:

    【解决方案2】:

    错误表明变量 start 在您的 element.onkeyup 中是 undefined,也就是说,在您的函数中。如果该变量不存在,则无法对其进行任何计算。

    基本上,你有一个范围问题,变量(let和const)只存在于它们被声明的本地,所以start只存在于一个@中987654321@。与变量 end 相同。

    要修复它,理想的方法是在顶部声明变量(startend),在您的 if's 之前,一起声明到 value。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 2011-09-12
      • 2021-06-28
      • 2013-04-30
      • 1970-01-01
      相关资源
      最近更新 更多