LoginSignup
0
0

More than 3 years have passed since last update.

【jQuery】jQueryで、隣り合うセルの色が異なる九九表を作成せよ。

Last updated at Posted at 2021-05-16

1・全ての九九の計算式にセルをつける
2・答えが奇数の行には色をつける
3・CSSについては以下を使うこと

table, tr, td {
    border: solid 1px;
}

td {
    padding: 5px;
}

.even {
    background-color: #F5F5F5;
}

image.png

<!DOCTYPE html>
<html>
<head>
<title>九九表</title>
<style>
table, tr, td {
    border: solid 1px;
}
td {
    padding: 5px;
}
.even {
    background-color: #F5F5F5;
}


</style>
<script src="jquery-3.6.0.min.js"></script>
<script>
  $(function(){
      for(var j=1; j<=9; j++){
        $('#table').append('<tr></tr>');
        for(var i=1; i<=9; i++){
            if ( i <= 9 ) {
              $('tr:last-child').append('<td>' +  i + '*' + j + '=' + i*j + '</td>' );
            }else {
              $('tr:last-child').append('<td>' +  i + '*' + j + '=' + i*j + '</td>');
            }
          }
      }
      $('td:even').addClass('even');
});
</script>
</head>
<body>
  <table id="table">
  <caption>九九表</caption>
  </table>
</body>
</html>

・補足

1・【td:even】では、0からはじまる連番でかぞえて偶数番目のtd要素がすべてマッチする。
2・【tr:last-child】では、trの子供(td)を最後に追加するってこと

0
0
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
0
0