index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const body = document.querySelector('body')
  2. let table = document.createElement("table")
  3. table.style.cursor = 'pointer'
  4. table.setAttribute('border', '1')
  5. table.setAttribute('frame', 'void')
  6. body.insertBefore(table, body.childNodes[0])
  7. let cells = []
  8. let wrappFunc = (cells) => {
  9. let currColor = []
  10. let currCells = []
  11. return (td, i, j) => {
  12. td.onmouseover = function() {
  13. currCells = cells.filter(item => item.cellIndex === j - 1 || item.parentElement.rowIndex === i - 1)
  14. for (const el of currCells) {
  15. currColor.push(el.style.backgroundColor)
  16. el.style.backgroundColor = 'red'
  17. }
  18. }
  19. td.onmouseout = function() {
  20. for (const el of currCells) {
  21. let color = currColor.shift()
  22. el.style.backgroundColor = color
  23. }
  24. }
  25. }
  26. }
  27. let innerFunc = wrappFunc(cells)
  28. for (let i = 1; i < 10; i++) {
  29. let tr = document.createElement("tr")
  30. if (i % 2 === 0) {
  31. tr.style.backgroundColor = '#CFCFCF'
  32. }
  33. table.appendChild(tr)
  34. for (let j = 1; j < 10; j++) {
  35. let td = document.createElement("td")
  36. cells.push(td)
  37. td.setAttribute('align', 'center')
  38. td.setAttribute('width', '40px')
  39. td.setAttribute('height', '40px')
  40. tr.appendChild(td)
  41. td.innerText = `${i*j}`
  42. innerFunc(td, i, j)
  43. }
  44. }