LoginSignup
13
10

More than 5 years have passed since last update.

NodeList に map メソッドがない(call の使い方)

Posted at

querySelectorAll でノードを取得したら Array でなく NodeList なので map メソッドがない。
調べたら call メソッドを使うことで解決できた。

call_sample.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>call sample</title>
    <script type="text/javascript">
    addEventListener('DOMContentLoaded', function () {
        var elms = document.querySelectorAll('tr>td:nth-child(2)');
        var names = Array.prototype.map.call(elms, function (elm) {
            return elm.firstChild.data;
        });
        console.log(names);
    });
    </script>
  </head>
  <body>
    <table>
      <tr>
        <th>作家名
        <th>作家名読み
      <tr>
        <td>太宰 治
        <td>だざい おさむ
      <tr>
        <td>小泉 八雲
        <td>こいずみ やくも
      <tr>
        <td>夢野 久作
        <td>ゆめの きゅうさく
  </body>
</html>
13
10
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
13
10