8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NamedNodeMap の要素へのアクセス

Posted at

DOM 要素の attributes などは、キーでアクセスできる通常の Object ではなく、NamedNodeMap という配列のようなデータ構造になっています。

まず、配列のようにインデックスでのアクセス。小要素は namevalue というプロパティを持っています。

for (var i = 0; i < elem.attributes.length; i++) {
  var node = elem.attributes[i],
      key = node.name,
      value = node.value;
}

それからキー名でのアクセス。Chrome なら

var title = elem.attributes['title'].value;

または

var title = elem.attributes.title.value;

とできますが、IE ではエラーが出ます。getNamedItem() を使うのが正しいのでした。

var title = elem.attributes.getNamedItem('title').value;
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?