2016-10-29 3 views
0

私は、複数のtd要素を使って、forEachdocument.querySelectorAllを使って繰り返しています。このループ内で要素のnameの値にアクセスするにはどうすればよいですか?JSのforEachで要素を反復処理するときに要素の名前を取得するにはどうすればよいですか?

これは私が現時点で試みていることであり、未定義に戻っています。

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 

答えて

3

変更

console.log(item.name); 

to:

console.log(item.getAttribute("name")) 
関連する問題