main.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 k = 0; k <= 10; k++) {
  8. let th = document.createElement('th')
  9. th.className = 'th'
  10. th.append(k)
  11. tr.append(th)
  12. }
  13. } else {
  14. for (let colIndex = 0; colIndex <= 10; colIndex++) {
  15. let td = document.createElement('td')
  16. td.onmouseover = function () {
  17. let array = [...this.parentElement.parentElement.children]
  18. for (let row = 0; row <= tr.rowIndex; row++) {
  19. for (let cell = 0; cell <= td.cellIndex; cell++) {
  20. if (cell === td.cellIndex) {
  21. [...array[row].children][cell].style.backgroundColor = '#fffbcc'
  22. }
  23. [...this.parentElement.children][cell].style.backgroundColor = '#fffbcc'
  24. }
  25. }
  26. this.style.backgroundColor = 'red'
  27. }
  28. td.onmouseout = function () {
  29. this.style.backgroundColor = ''
  30. let array = [...this.parentElement.parentElement.children]
  31. for (let cell = 0; cell <= tr.rowIndex; cell++) {
  32. for (let row = 0; row <= td.cellIndex; row++) {
  33. [...array[cell].children][row].style.backgroundColor = ''
  34. }
  35. }
  36. }
  37. if (colIndex === 0) {
  38. let th = document.createElement('th')
  39. th.className = 'th'
  40. th.append((rowIndex) * (colIndex + 1))
  41. tr.append(th)
  42. } else if (colIndex === rowIndex) {
  43. td.className = 'tdtd'
  44. td.append((rowIndex) * (colIndex))
  45. tr.append(td)
  46. } else {
  47. td.append((rowIndex) * (colIndex))
  48. tr.append(td)
  49. }
  50. }
  51. }
  52. }
  53. root.append(table)
  54. plus.addEventListener('click', () => {
  55. result.value = +numOne.value + +numTwo.value
  56. })
  57. minus.addEventListener('click', () => {
  58. result.value = +numOne.value - +numTwo.value
  59. })
  60. division.addEventListener('click', () => {
  61. result.value = +numOne.value / +numTwo.value
  62. })
  63. multiplication.addEventListener('click', () => {
  64. result.value = +numOne.value * +numTwo.value
  65. })
  66. numOne.addEventListener('input', () => {
  67. result.value = +numOne.value + +numTwo.value
  68. })
  69. numTwo.addEventListener('input', () => {
  70. result.value = +numOne.value + +numTwo.value
  71. })