简体   繁体   English

为什么会出现“无法读取未定义的html属性”?

[英]Why am I getting “Cannot read property of html undefined”?

I am building a simple counter to practice closures in JS, and I can't seem to get past this error. 我正在建立一个简单的计数器来练习JS中的闭包,但似乎无法克服此错误。 In my mind setting the variable elem to the id of canv should work, but something is obviously not connecting. 在我看来,将变量elem设置为canv的id应该可以,但是显然没有任何联系。 What am I doing wrong here? 我在这里做错了什么?

My HTML: 我的HTML:

<body>
  <div id="canv">

  </div>
</body>

My JS: 我的JS:

function setup(delay) {
  var el = document.getElementById('canv');

  counterFn(el, delay);
}

function counterFn(el, delay) {
  var counter = 0;

  setInterval(getTime, delay);

  function getTime(el) {
    el.innerHTML(counter);
    counter++;
  }
}

setup(500);

This all feels very elementary but I can't seem to figure out what I am doing wrong. 这一切感觉非常基础,但是我似乎无法弄清楚自己在做什么错。

Here's a JS fiddle. 这是一个JS小提琴。 https://jsfiddle.net/8sjaqdrh/2/ https://jsfiddle.net/8sjaqdrh/2/

The thing that caused the error is because el is not defined within the scope of getTime . 导致该错误的原因是,未在getTime范围内定义el

In getTime's parent scope counterFn , el is defined because you gave it a value when calling it in counterFn(el, delay) inside setup function, which is el = document.getElementById('canv') . 在getTime的父作用域counterFn ,定义了el是因为在setup函数el = document.getElementById('canv')中的counterFn(el, delay)调用它时给了它一个值。

However in the scope of getTime , you did not provide el with a value. 但是,在getTime范围内,没有为el提供值。 So el becomes undefined . 所以el变得undefined

Another problem with the code is the incorrect usage of innerHTML . 代码的另一个问题是innerHTML使用不正确。

Just removing the parameter from function getTime(el){... to function getTime(){... will do. 只需将参数从function getTime(el){...删除到function getTime(el){...即可function getTime(){...

As for innerHTML , it is not a function. 至于innerHTML ,它不是一个函数。 So it can't be called like innerHTML(counter) . 因此不能像innerHTML(counter)那样调用它。

The correct way is to directly assign a value to it like innerHTML = counter . 正确的方法是直接为它分配一个值,例如innerHTML = counter

 function setup(delay) { var el = document.getElementById('canv'); counterFn(el, delay); } function counterFn(el, delay) { var counter = 0; setInterval(getTime, delay); // Corrected code: function getTime() { el.innerHTML = counter; counter++; } /* Your code function getTime(el){ el.innerHTML(counter); counter++; } */ } setup(500); 
 <div id="canv"></div> 

One potential issue here is that your trying to get the canvas before it exist in the DOM, you will want to wrap your code in a window.onload event to prevent that from happening. 这里一个潜在的问题是,您试图在画布在DOM中存在之前获取它,您将希望将代码包装在window.onload事件中,以防止发生这种情况。

Next for the code bellow here: 下面是下面的代码:

setInterval(getTime, delay);

You define getTime as expecting a parameter but did not pass one, instead you will want do to: 您将getTime定义为期望参数,但未传递参数,而是需要执行以下操作:

setInterval(function(){ getTime(el); }, wait);

Finally el.innerHTML(counter); 最后是el.innerHTML(counter); should be el.innerHTML = counter; 应该是el.innerHTML = counter; .

Working Fiddle here 在这里工作小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 为什么在此示例中出现“无法读取未定义的属性&#39;文本&#39;”? - Why am I getting “Cannot read property 'text' of undefined” in this example? 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript) - Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript) 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”? - why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign? 尝试访问嵌入的Google地图时,为什么会出现无法读取未定义的属性“ getCenter”的问题? - Why am I getting Cannot read property 'getCenter' of undefined when trying to access my embed google map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM