DOM1.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Таблиця множення + підсвічування ячейки ---------------------------------------------------------------------------------;
  2. {
  3. let myTable = document.createElement("table");
  4. document.body.append(myTable);
  5. table.className = "Cross";
  6. for (let i = 1; i <= 10; i++) {
  7. let tr = document.createElement("tr");
  8. myTable.appendChild(tr);
  9. i % 2 === 0
  10. ? (tr.style.backgroundColor = "green")
  11. : (tr.style.backgroundColor = "red");
  12. for (let j = 1; j <= 10; j++) {
  13. let td = document.createElement("td");
  14. td.onmouseover = function (event) {
  15. td.style.backgroundColor = "blue";
  16. };
  17. td.onmouseout = function (event) {
  18. td.style.backgroundColor = "";
  19. };
  20. td.style.border = "1px solid black";
  21. td.style.textAlign = "center";
  22. td.style.padding = "10px";
  23. tr.appendChild(td);
  24. td.innerText = i * j;
  25. }
  26. }
  27. }
  28. // Підсвічування строки та стовпця----------------------------------------------------------------------------------------------;
  29. {
  30. let yourTable = document.createElement("table");
  31. document.body.append(yourTable);
  32. for (let i = 1; i <= 10; i++) {
  33. let tr = document.createElement("tr");
  34. yourTable.appendChild(tr);
  35. for (let j = 1; j <= 10; j++) {
  36. let td = document.createElement("td");
  37. td.style.border = "1px solid black";
  38. yourTable.style.borderCollapse = "collapse";
  39. td.style.textAlign = "center";
  40. td.style.padding = "10px";
  41. tr.appendChild(td);
  42. td.innerText = i * j;
  43. }
  44. }
  45. const changeBg = (event, color) => {
  46. const target = event.target;
  47. if (target.tagName === "TD") {
  48. target.style.backgroundColor = color;
  49. target.parentNode.style.backgroundColor = color;
  50. const index = event.srcElement.cellIndex;
  51. const rows = document.querySelectorAll("tr");
  52. rows.forEach(
  53. (tr) => (tr.childNodes[index].style.backgroundColor = color)
  54. );
  55. }
  56. };
  57. const defaultCol = (event) => {
  58. myTable.childNodes.style.backgroundColor = "";
  59. };
  60. myTable.onmouseover = (event) => changeBg(event, "yellow");
  61. myTable.onmouseout = (event) => changeBg(event, "");
  62. }
  63. // Calc------------------------------------------------------------------------------------------------------------------------------;
  64. {
  65. let calculator = document.getElementById("calc");
  66. let breakFa = document.getElementById("breakfast");
  67. let lunch = document.getElementById("lunch");
  68. let dinner = document.getElementById("dinner");
  69. let result = document.getElementById("res");
  70. calculator.onclick = function () {
  71. result.innerText = +breakFa.value + +lunch.value + +dinner.value;
  72. result.style.backgroundColor = "green";
  73. };
  74. }
  75. // Calc Live-------------------------------------------------------------------------------------------------------------------------;
  76. {
  77. let breakFa = document.getElementById("breakfast");
  78. let lunch = document.getElementById("lunch");
  79. let dinner = document.getElementById("dinner");
  80. let result = document.getElementById("res");
  81. function calc() {
  82. result.innerText = +breakFa.value + +lunch.value + +dinner.value;
  83. result.style.backgroundColor = "";
  84. }
  85. breakFa.oninput = calc;
  86. lunch.oninput = calc;
  87. dinner.oninput = calc;
  88. }