main.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. let table = document.createElement('table')
  2. for (let rowIndex = 0; rowIndex <= 10; rowIndex++) {
  3. let tr = document.createElement('tr')
  4. rowIndex % 2 === 0 ? tr.className = "tr-odd" : tr.className = "tr-even"
  5. table.append(tr)
  6. if (rowIndex === 0) {
  7. for (let t = 0; t <= 10; t++) {
  8. let th = document.createElement('th')
  9. th.className = 'th'
  10. th.innerText = t
  11. tr.append(th)
  12. }
  13. } else {
  14. for (let colIndex = 0; colIndex <= 10; colIndex++) {
  15. let td = document.createElement('td')
  16. // подсвечивает строку и столбец до указаной клетки
  17. function lightTd(e) {
  18. let arrayParentChildren = [...this.parentElement.parentElement.children]
  19. for (let row = 0; row <= tr.rowIndex; row++) {
  20. for (let cell = 0; cell <= td.cellIndex; cell++) {
  21. if (e.type === 'mousemove') {
  22. if (cell === td.cellIndex) {
  23. [...arrayParentChildren[row].children][cell].style.backgroundColor = '#9e90ff'
  24. }
  25. [...this.parentElement.children][cell].style.backgroundColor = '#9e90ff'
  26. this.style.backgroundColor = 'red'
  27. } else {
  28. [...arrayParentChildren[row].children][cell].style.backgroundColor = ''
  29. }
  30. }
  31. }
  32. }
  33. td.onmousemove = lightTd
  34. td.onmouseout = lightTd
  35. if (colIndex === 0) {
  36. let th = document.createElement('th')
  37. th.className = 'th'
  38. th.innerText = (rowIndex) * (colIndex + 1)
  39. tr.append(th)
  40. } else if (colIndex === rowIndex) {
  41. td.className = 'tdtd'
  42. td.innerText = (rowIndex) * (colIndex)
  43. tr.append(td)
  44. } else {
  45. td.innerText = (rowIndex) * (colIndex)
  46. tr.append(td)
  47. }
  48. }
  49. }
  50. }
  51. root.append(table)
  52. plus.addEventListener('click', () => {
  53. result.value = +numOne.value + +numTwo.value
  54. })
  55. minus.addEventListener('click', () => {
  56. result.value = +numOne.value - +numTwo.value
  57. })
  58. division.addEventListener('click', () => {
  59. result.value = +numOne.value / +numTwo.value
  60. })
  61. multiplication.addEventListener('click', () => {
  62. result.value = +numOne.value * +numTwo.value
  63. })
  64. numOne.addEventListener('input', () => {
  65. result.value = +numOne.value + +numTwo.value
  66. })
  67. numTwo.addEventListener('input', () => {
  68. result.value = +numOne.value + +numTwo.value
  69. })