Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于in-loop无法设置未定义的属性_Javascript_For Loop_Properties_Undefined_Console.log - Fatal编程技术网

Javascript 对于in-loop无法设置未定义的属性

Javascript 对于in-loop无法设置未定义的属性,javascript,for-loop,properties,undefined,console.log,Javascript,For Loop,Properties,Undefined,Console.log,var table=document.querySelectorAll(“.number”); 函数seppuku(){ 对于(让我在表中){ 表[i].style.color=“红色”; } } seppuku() 1 2. 3使用让i进入表将i的最后一个元素作为长度,因此结果是表['length'].style.color=“red”不正确,因为没有元素表['length']并返回未定义的。更好的方法是在表的长度中循环,它将是0、1和2 var table=document.querySe

var table=document.querySelectorAll(“.number”);
函数seppuku(){
对于(让我在表中){
表[i].style.color=“红色”;
}
}
seppuku()
1
2.

3
使用
让i进入表
i的最后一个元素作为
长度
,因此结果是
表['length'].style.color=“red”
不正确,因为没有元素
表['length']
并返回
未定义的
。更好的方法是在
表的
长度
中循环,它将是0、1和2

var table=document.querySelectorAll(“.number”);
函数seppuku(){

对于(var i=0;i使用
let i in table
i
的最后一个元素设置为
length
,因此结果为
table['length'].style.color=“red”
这是不正确的,因为没有元素
table['length']
并返回
未定义的
。更好的方法是通过
长度
,该长度为0、1和2

var table=document.querySelectorAll(“.number”);
函数seppuku(){

for(var i=0;i您在
语句中使用了
for…,该语句迭代对象的可枚举属性。在本例中,
对象包含以下可枚举属性

{
  "0": <div class="numbers">1</div>,
  "1": <div class="numbers">2</div>,
  "2": <div class="numbers">3</div>,
  "length": 3,
  "item": function item() { [native code] },
  "entries": function entries() { [native code] },
  "forEach": function forEach() { [native code] },
  "keys": function keys() { [native code] },
  "values": function values() { [native code] }
}
1
2.

3
您在
语句中对…使用了
,该语句迭代对象的可枚举属性。在这种情况下,
对象包含以下可枚举属性

{
  "0": <div class="numbers">1</div>,
  "1": <div class="numbers">2</div>,
  "2": <div class="numbers">3</div>,
  "length": 3,
  "item": function item() { [native code] },
  "entries": function entries() { [native code] },
  "forEach": function forEach() { [native code] },
  "keys": function keys() { [native code] },
  "values": function values() { [native code] }
}
1
2.

3
在中使用简单的for循环,而不是for


1.
2.
3.
函数seppuku(){
var table=document.queryselectoral(“.number”);

for(设i=0;i使用简单的for循环,而不是for..in


1.
2.
3.
函数seppuku(){
var table=document.queryselectoral(“.number”);

for(让i=0;i-。而不是for in-loop使用for-loop尝试
console.log(table)
,你就会明白你的循环在迭代什么,因为
document.queryselectoral(.numbers”)
的结果是一个类似数组的对象,如果你想使用for循环,你必须转换成array
var table=document.querySelectorAll(.numbers));
table=Array.prototype.slice.call(table)
请参见-。请尝试
console.log(table)
而不是for in-loop,这样您就可以了解循环的迭代内容,因为
document.querySelectorAll(.numbers)的结果
这是一个类似数组的对象,如果你想使用for循环,你必须转换为array
var table=document.queryselectoral(“.numbers”);
table=array.prototype.slice.call(table)
谢谢。我只是想深入了解ES6(我知道典型的“for循环”方法)。但是,是的,我肯定会使用工作方法;)谢谢。我只是想深入了解ES6(我知道典型的“for-loop”方法)。但是,是的,我肯定会使用工作方法;)谢谢。我只是想深入了解ES6(我知道典型的“for-loop”方法)。但是,是的,我肯定会使用工作方法;)我添加了更多的解释。请让我知道它是否对您有帮助谢谢。我只是想深入了解ES6(我知道典型的“for循环”方法)。但是,是的,我肯定会使用工作方法;)我添加了更多的解释。请让我知道它是否对您有帮助