LoginSignup
1
1

More than 5 years have passed since last update.

javascriptでtableをループ その2-1

Last updated at Posted at 2014-11-11

HTML直書きしないことを想定して回してみたtdバージョン

html
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>外部jsからtbody内を繰り返し処理したい</title>
</head>
<body>

<table>
  <thead>
    <tr>
      <td></td>
    </tr>
  </thead>
  <tbody>
    <tr id="tbodyID">
      <!--ここを回す-->
    </tr>
  </tbody>
</table>
</body>
</html>
jsvascript
<script>
    /*ページ(DOM)読み込み後に実行*/
    window.onload = function(){

        /*tbodyのIDを取得(この中で処理させる)*/
        var tbody = document.getElementById('tbodyID');

        for (i = 0; i<5; i++){
            //tr エレメントを新規作成(ただ生成するだけ)
            var tr = document.createElement('tr');
            tr.innerHTML='こんにちは'+i;
            //tr エレメントをtbody内に追加(ここではじめて表示される)
            tbody.appendChild(tr);
        }

    };
</script>
1
1
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
1
1