当前位置: 首页 > 知识库问答 >
问题:

司在JavaScript给出未定义的错误

宰父衡
2023-03-14

我试图计算从用户开始打字到他们结束打字之间的时间。因此,我用日期命名了两个常量。现在() 函数。每当我运行程序时,它总是给出错误:ReferenceError:start未在HTMLInputElement中定义。要素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 () {
  var value = element.value;

   if (value.includes("m")) {
     let start = Date.now();
  } 

  if (value.includes("man")) {
    document.getElementById('word-1').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-1').style.backgroundColor = "red";
  }

  if (value.includes("man become")) {
    document.getElementById('word-2').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-2').style.backgroundColor = "red";
  }

  if (value.includes("man become as")) {
    document.getElementById('word-3').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-3').style.backgroundColor = "red";
  }

  if (value.includes("man become as and")) {
    document.getElementById('word-4').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-4').style.backgroundColor = "red";
  }

  if (value.includes("man become as and through")) {
    document.getElementById('word-5').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-5').style.backgroundColor = "red";
  }

  if (value.includes("man become as and through find")) {
    document.getElementById('word-6').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-6').style.backgroundColor = "red";
  }

  if (value.includes("man become as and through find would")) {
    document.getElementById('word-7').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-7').style.backgroundColor = "red";
  }

  if (value.includes("man become as and through find would here")) {
    document.getElementById('word-8').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-8').style.backgroundColor = "red";
  }

  if (value.includes("man become as and through find would here and")) {
    document.getElementById('word-9').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-9').style.backgroundColor = "red";
  }

  if (value.includes("man become as and through find would here and before")) {
    document.getElementById('word-10').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-10').style.backgroundColor = "red";
  }

   if (value.includes("man become as and through find would here and before")) {
     let end = Date.now();
 
  }
  let millis = start/end;

  console.log(`seconds elapsed = ${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>


共有2个答案

艾雪风
2023-03-14

该错误表示变量start在element.onkeyup中未定义,即在函数中。如果变量不存在,就不可能用它做任何计算。

基本上,您有一个作用域问题,变量(let和const)只存在于声明它们的局部变量中,因此start只存在于一个if(){}中。变量端也是如此。

要解决这个问题,理想的做法是在顶部的if之前声明变量(开始和结束),并将其与值一起声明。

慕承允
2023-03-14

声明变量“start”的位置与使用该变量时不在同一范围内。这意味着

let millis = start/end;

无法看到开始。

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

 类似资料:
  • 问题内容: 基本上,我使用javascript通过以下方式从Google Play商店中抓取数据: 一请求 2麦片 3-QueryString 我使用了来自Github的Google Market API,其使用require的方式如下: 但我得到以下 ReferenceError:需求未定义… 因此,我在javascript中没有要求,这对我来说不是新的,还是与众不同。 问题答案: Requir

  • 问题内容: 注意:根据 ECMAScript5.1,第15.1.1.3节,window.undefined是只读的。 现代浏览器正确地实现了这一点。 例如:Safari 5.1,Firefox 7,Chrome 20等。 Undefined仍可在以下位置更改:Chrome 14,… 原来,问题的原因是 如果“对象”中没有属性“ x”,则返回false。 我通过在两个Facebook函数中用规则相等

  • 问题内容: 我正在Ubuntu平台中使用jquery,javascript,php。在页面中,我通过jquery发送一个ajax请求到php文件并获取响应文本。该程序已在Windows-(Wamp)平台和联机环境中成功运行。但是在Ubuntu中,我遇到了JavaScript错误。代码如下… 在Ubuntu中,我收到此错误, “ Uncaught ReferenceError:$未定义” 。该错误显

  • 问题内容: 在开发机器上,我的网站运行正常,但是在托管服务器上我遇到了javascript’Sys ‘未定义 错误,并且我的AJAX无法正常工作。 我在上面做了一个示例页面和脚本管理器,请仔细研究。 http://nexapps.com/default2.aspx 按钮和文本框位于AJAX updatepanel中,但是当单击按钮页面时,将回发完整。 注意:托管服务器-IIS7,但我的其他网站在同

  • 我没有正确安装吗?我忽略了什么?

  • 问题内容: 我看了一些有关主干js的视频。这是直接来自视频的示例。是从2012年开始的,所以我认为主干规则/库已经更改,但是我不知道为什么这暂时不起作用。在视频中,该人演示了它在JS小提琴中运行的过程,但我无法使其正常工作。(我在JS Fiddle中包括了必要的库,即下划线,主干和jQuery) 问题答案: 您曾经能够一口气解析并填写Underscore模板,如下所示: 但是从Underscore