Browse Source

added hw9, refactoring table (function lightTd)

makstravm 3 năm trước cách đây
mục cha
commit
ce276915bb
4 tập tin đã thay đổi với 110 bổ sung1 xóa
  1. 1 1
      HW8/index.html
  2. 20 0
      HW9/index.html
  3. 51 0
      HW9/main.js
  4. 38 0
      HW9/style.css

+ 1 - 1
HW8/index.html

@@ -13,7 +13,6 @@
 
 <body>
   <div id='root'></div>
-
   <div class="calc">
     <div class="calc__inner">
       <input id='numOne' type="number">
@@ -28,6 +27,7 @@
     </div>
   </div>
   <script src="main.js"></script>
+  
 </body>
 
 </html>

+ 20 - 0
HW9/index.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html lang="en-ru">
+
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <link rel="stylesheet" href="style.css">
+  <title>HW9</title>
+
+</head>
+
+
+<body>
+  <div id='root'></div>
+  <script src="main.js"></script>
+  
+</body>
+
+</html>

+ 51 - 0
HW9/main.js

@@ -0,0 +1,51 @@
+let table = document.createElement('table')
+for (let rowIndex = 0; rowIndex <= 10; rowIndex++) {
+  let tr = document.createElement('tr')
+  rowIndex % 2 === 0 ? tr.className = "tr-odd" : tr.className = "tr-even"
+  table.append(tr)
+  if (rowIndex === 0) {
+    for (let t = 0; t <= 10; t++) {
+      let th = document.createElement('th')
+      th.className = 'th'
+      th.append(t)
+      tr.append(th)
+    }
+  } else {
+    for (let colIndex = 0; colIndex <= 10; colIndex++) {
+      let td = document.createElement('td')
+      function findChild(e) {
+        let arrayParentChildren = [...this.parentElement.parentElement.children]
+        for (let row = 0; row <= tr.rowIndex; row++) {
+          for (let cell = 0; cell <= td.cellIndex; cell++) {
+            if (e.type === 'mousemove') {
+              if (cell === td.cellIndex) {
+                [...arrayParentChildren[row].children][cell].style.backgroundColor = '#fffbcc'
+              }
+              [...this.parentElement.children][cell].style.backgroundColor = '#fffbcc'
+              this.style.backgroundColor = 'red'
+            } else {
+              [...arrayParentChildren[row].children][cell].style.backgroundColor = ''
+            }
+          }
+        }
+      }
+      td.onmousemove = findChild
+      td.onmouseout = findChild
+      if (colIndex === 0) {
+        let th = document.createElement('th')
+        th.className = 'th'
+        th.append((rowIndex) * (colIndex + 1))
+        tr.append(th)
+      } else if (colIndex === rowIndex) {
+        td.className = 'tdtd'
+        td.append((rowIndex) * (colIndex))
+        tr.append(td)
+      } else {
+        td.append((rowIndex) * (colIndex))
+        tr.append(td)
+      }
+    }
+  }
+}
+root.append(table)
+

+ 38 - 0
HW9/style.css

@@ -0,0 +1,38 @@
+body {
+  max-width: 1200px;
+  padding: 15px;
+  box-sizing: border-box;
+  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
+}
+table {
+  text-align: center;
+  border-spacing: 0;
+  font-size: 20px;
+  width: 50%;
+  margin: 0 auto;
+  height: 450px;
+  margin-bottom: 50px;
+}
+
+td,
+th {
+  border: 1px solid #000;
+}
+td {
+  cursor: pointer;
+}
+
+.th,
+.th .th {
+  background-color: rgba(48, 48, 48, 0.867);
+  color: rgb(72, 255, 0);
+  font-weight: 700;
+}
+.tdtd {
+  background-color: rgba(0, 255, 242, 0.582);
+  color: rgb(3, 3, 43);
+  font-weight: 500;
+}
+.tr-even {
+  background-color: rgba(233, 233, 233, 0.867);
+}