index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Таблица умножения */
  2. let table = document.createElement("table")
  3. document.body.append(table)
  4. for (let i = 0; i < 10; i++) {
  5. let tr = document.createElement("tr")
  6. table.append(tr)
  7. for (let j = 0; j < 10; j++) {
  8. let td = document.createElement("td")
  9. tr.append(td)
  10. if (i === 0 || j === 0) {
  11. td.innerText = i + j
  12. } else {
  13. td.innerText = i * j
  14. }
  15. td.style.border = "1px solid black"
  16. td.style.padding = "7px"
  17. }
  18. }
  19. /* Подсветить ячейку */
  20. let backlight = table.querySelectorAll("td")
  21. let row = table.querySelectorAll("tr")
  22. let activeCellIndex = null;
  23. backlight.forEach(tdOn => {
  24. tdOn.onmousemove = function () {
  25. this.style.backgroundColor = "red"
  26. activeCellIndex = this.cellIndex;
  27. }
  28. })
  29. backlight.forEach(tdOut => {
  30. tdOut.onmouseout = function () {
  31. this.style.backgroundColor = "white"
  32. activeCellIndex = null
  33. }
  34. }
  35. )
  36. /* Подсветить стену и столбец */
  37. row.forEach(rowOn => rowOn.onmousemove = function () {
  38. this.querySelectorAll("td").forEach(elem => elem.style.borderColor = 'red');
  39. row.forEach(elem => elem.cells[activeCellIndex].style.borderColor = 'red')
  40. })
  41. row.forEach(rowOn => rowOn.onmouseout = function () {
  42. table.querySelectorAll("td").forEach(elem => elem.style.borderColor = 'black');
  43. })
  44. /* Расчет */
  45. let calc1 = document.getElementById("calc1")
  46. let calc2 = document.getElementById("calc2")
  47. calc.onclick = function () {
  48. alert(+calc1.value + +calc2.value);
  49. };
  50. /* Расчет в реальном времени */
  51. function calc() {
  52. result.value = (+calc1.value) + (+calc2.value)
  53. }
  54. calc1.oninput = calc
  55. calc2.oninput = calc