【发布时间】:2015-08-13 11:12:10
【问题描述】:
我正在用cheerio 运行节点。我有这个函数需要显示数组中包含的元素的属性值。
for (var row = 0; row < array.length; row++) {
console.log("Row Length: " + array[row].length);
for (var col = 0; col < array[row].length; col++) {
console.log("Inside:");
console.log(array[row][col].getAttribute('val'));
}
}
我的数组包含使用 push 填充的列表项
array.push($('[title!="CATA"][collection="items"]'));
我得到了“行长度”的所需输出,它显示“内部”,但随后显示“类型错误:未定义不是函数”。我也尝试过使用 .attr('val') 但同样的错误。
执行 console.log(array[row][col]) 给我以下。
{ type: 'tag',
name: 'li',
attribs:
{ id: 'Amber',
collection: 'items',
title: 'disk',
val: '10|9|2|10|7|13|2|10|12|2|2|5|3|3|5|2|8|7|5|10|4|6|6|3|4|6|2|8|11|2|10|7|9|9|10|8' },
children:
[ { data: 'disk : 10|9|2|10|7|13|2|10|12|2|2|5|3|3|5|2|8|7|5|10|4|6|6|3|4|6|2|8|11|2|10|7|9|9|10|8',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
next:
{ type: 'tag',
name: 'li',
attribs:
{ id: 'Amber',
collection: 'items',
title: 'usb',
val: '3|3|2|4|2|4|3|3|3|13|5|13|2|13|2|5|5|3|4|3|6|3|2|2|5|13|2|3|2|13|2|13|2|2|13|4' },
children: [ [Object] ],
next:
.
.
.
我要做的就是访问“val”。
【问题讨论】:
-
你打印了 array[0][0] 元素吗?您正在研究 HTML 元素,但我怀疑数组没有在数组 [0][0] 索引处保存 HTML 元素。
标签: javascript arrays node.js cheerio