Vitalii Polishchuk 3 år sedan
förälder
incheckning
c4e4737ca1

+ 4 - 0
js/09-js-dom-cities_countries-table-closures/css/main.css

@@ -0,0 +1,4 @@
+td {
+    padding: 10px;
+    border: 1px solid #000;
+}

+ 17 - 0
js/09-js-dom-cities_countries-table-closures/index.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<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">
+    <title>Document</title>
+    <link rel="stylesheet" href="css/main.css">
+</head>
+
+<body>
+
+    <script src="js/main.js"></script>
+</body>
+
+</html>

+ 66 - 0
js/09-js-dom-cities_countries-table-closures/js/main.js

@@ -0,0 +1,66 @@
+fetch('https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.json')
+    .then(res => res.json())
+    .then(json => {
+        let countrySelect = document.createElement("select");
+        let citySelect = document.createElement("select");
+
+        for (let countryName in json) {
+            let countrySelectOption = document.createElement("option")
+            countrySelectOption.innerText = countryName;
+            countrySelect.append(countrySelectOption)
+        }
+
+        countrySelect.onchange = () => {
+            citySelect.innerHTML = "";
+
+            let cityName = json[countrySelect.value]
+
+            for (let key in cityName) {
+                let citySelectOption = document.createElement("option")
+                citySelectOption.innerText = cityName[key];
+                citySelect.append(citySelectOption)
+            }
+        }
+
+        document.body.append(countrySelect);
+        document.body.append(citySelect);
+    })
+
+
+
+let table = document.createElement("table");
+
+for (let i = 0; i <= 9; i++) {
+    let tr = document.createElement("tr");
+
+    for (let j = 0; j <= 9; j++) {
+        let td = document.createElement("td");
+
+        if (i == 0 || j == 0) {
+            td.innerText = i + j;
+        } else {
+            td.innerText = i * j;
+        }
+        tr.appendChild(td);
+
+        td.onmouseover = function (event) {
+            for (let i = 0; i < table.children.length; i++) {          //перебор всех тр и покраска тд с определенным индексом
+                table.children[i].children[j].style.background = "yellow";
+            }
+            tr.style.background = "red"  //красим строку
+            td.style.background = "orange";                  //красим саму ячейку
+        }
+
+        td.onmouseout = function (event) {
+            for (let i = 0; i < table.children.length; i++) {
+                table.children[i].children[j].style.background = "";
+            }
+            tr.style.background = ""
+            td.style.background = "";
+        }
+    }
+    table.appendChild(tr);
+}
+document.body.appendChild(table);
+
+table.style.float = "left";