我使用td
和forEach
迭代多个document.querySelectorAll
元素。如何在此循环中访问元素的name
值?
这就是我现在正在尝试的,而且它还没有被定义。
HTML:
<td name="monday" class="from">foobar</td>
<td name="tuesday" class="from">foobar</td>
<td name="wednesday" class="from">foobar</td>
<td name="thursday" class="from">foobar</td>
<td name="friday" class="from">foobar</td>
<td name="saturday" class="from">foobar</td>
<td name="sunday" class="from">foobar</td>
JS:
const myNodeList = document.querySelectorAll('.from'); // grabs some td elements
[].forEach.call(myNodeList, function (item) {
console.log(item.name);
});
结果:
undefined
undefined
undefined
undefined
undefined
undefined
undefined
答案 0 :(得分:3)
更改
console.log(item.name);
到:
console.log(item.getAttribute("name"))