dom_dom0400_005

属性を設定・変更するにはelem.( A )(‘data-id’,’42’)、属性を取り除くにはelem.( B )(‘data-id’)を用いる。

解答

A: setAttribute
B: removeAttribute

解説

属性APIは文字列ベースで任意属性に対応。プロパティ(elem.id等)と区別して考える。

使用例(サンプルコード)

[ html ]

<div id="box"></div>
<script>
  const box = document.getElementById('box');
  box.setAttribute('data-id', '42');
  box.removeAttribute('data-id');
</script>