简体   繁体   English

Javascript Uncaught TypeError:无法设置未定义的属性

[英]Javascript Uncaught TypeError: Cannot set property of undefined

I have a problem with my code in JavaScript, probably some stupid mistake but I can't find it... an error occurs. 我的JavaScript代码有问题,可能是一些愚蠢的错误,但我找不到它……发生错误。

Uncaught TypeError: Cannot set property 'innerHTML' of undefined 未捕获的TypeError:无法设置未定义的属性“ innerHTML”

Even though the code seems to do what it should (creates tasks which have a header of Task 1 , Task 2 , Task 3 and so on).. How can I fix that? 即使代码似乎在做应做的事情(创建带有Task 1Task 2Task 3等等标题的Task 1 )。我该如何解决? Maybe there is a shorter way to give my tasks numbered headers? 也许有一种较短的方法可以给我的任务编号标题?

document.addEventListener("DOMContentLoaded", function(){

  var add=document.querySelector("#addTaskButton");
  var tasklist=document.querySelector("#taskList");
  var clickcount=0;

  add.addEventListener("click",function(event){
   var newTask=document.createElement("li");
   tasklist.appendChild(newTask);
   var newh1=document.createElement("h1");
   newTask.appendChild(newh1);

  clickcount+=1;
   if(clickcount==1){
     var h1=document.querySelector("h1");
     h1.innerText="Task 1";
   }else if(clickcount>1){
     var hmore=document.getElementsByTagName("h1");
     for(var i=1;i<=hmore.length;i++){
       hmore[i].innerHTML="Task "+(i+1);
      }
   }

 })

});
 for(var i=1;i<=hmore.length;i++){ 

Array-like objects in JavaScript are zero indexed. JavaScript中类似数组的对象的索引为零。

The length is the number of items in the object. 长度是对象中的项目数。

This means that if you have an array with 3 items in it, they exist at indexes 0, 1, and 2. 这意味着,如果您有一个包含3个项目的数组,则它们分别位于索引0、1和2处。

So when i == hmore.length you have gone one beyond the end . 因此,当i == hmore.length您已经超越了终点

You need to test for < not <= 您需要测试<不是<=

暂无
暂无

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

相关问题 JavaScript Uncaught TypeError:无法设置未定义的属性“ -1” - JavaScript Uncaught TypeError: Cannot set property '-1' of undefined JavaScript - 未捕获的类型错误:无法设置未定义的属性 - JavaScript - Uncaught TypeError: Cannot set property of undefined Javascript 函数错误未捕获的类型错误:无法设置未定义的属性“目标” - Javascript function error Uncaught TypeError: Cannot set property 'target' of undefined Uncaught TypeError:无法在2D数组(JavaScript)上设置undefined的属性“ 0” - Uncaught TypeError: Cannot set property '0' of undefined on 2D array (JavaScript) Uncaught TypeError:无法设置未定义JavaScript的属性“ width” - Uncaught TypeError: Cannot set property 'width' of undefined, javascript Uncaught TypeError:无法设置未定义javascript的属性“ backgroundColor” - Uncaught TypeError: cannot set property 'backgroundColor' of undefined javascript Javascript:未捕获的TypeError:无法设置未定义的属性“ className” - Javascript: Uncaught TypeError: Cannot set property 'className' of undefined Javascript | 未捕获的TypeError:无法设置未定义的属性“颜色” - Javascript | Uncaught TypeError: Cannot set property 'color' of undefined javascript:未捕获的TypeError:无法读取未定义的属性“ set” - javascript: Uncaught TypeError: Cannot read property 'set' of undefined Javascript滑块:未捕获的TypeError:无法设置未定义的属性&#39;className&#39; - Javascript slider: Uncaught TypeError: Cannot set property 'className' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM