index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. tableMultDom()
  2. function tableMultDom() {
  3. const body = document.querySelector('body')
  4. let table = document.createElement("table")
  5. table.style.cursor = 'pointer'
  6. table.setAttribute('border', '1')
  7. table.setAttribute('frame', 'void')
  8. body.insertBefore(table, body.childNodes[0])
  9. for (let rowIndex = 1; rowIndex < 10; rowIndex++) {
  10. let tr = document.createElement("tr")
  11. if (rowIndex % 2 === 0) {
  12. tr.style.backgroundColor = '#CFCFCF'
  13. }
  14. table.appendChild(tr)
  15. for (let colIndex = 1; colIndex < 10; colIndex++) {
  16. let td = document.createElement("td")
  17. td.setAttribute('align', 'center')
  18. td.setAttribute('width', '40px')
  19. td.setAttribute('height', '40px')
  20. tr.appendChild(td)
  21. td.innerText = `${rowIndex*colIndex}`
  22. }
  23. }
  24. }
  25. // tableMultDomHighlight()
  26. function tableMultDomHighlight() {
  27. const cells = document.querySelectorAll('td')
  28. for (const cell of cells) {
  29. let currColor = null
  30. cell.onmouseover = function() {
  31. currColor = this.style.backgroundColor
  32. this.style.backgroundColor = 'red'
  33. }
  34. cell.onmouseout = function() {
  35. this.style.backgroundColor = currColor
  36. }
  37. }
  38. }
  39. tableMultDomHighlightRowCol()
  40. function tableMultDomHighlightRowCol() {
  41. const cells = Array.from(document.querySelectorAll('td'))
  42. for (const cell of cells) {
  43. let currColor = []
  44. let currCells = []
  45. cell.onmouseover = function() {
  46. let cellIndex1 = this.cellIndex
  47. let rowIndex1 = this.parentElement.rowIndex
  48. currCells = cells.filter(item => item.cellIndex === cellIndex1 || item.parentElement.rowIndex === rowIndex1)
  49. for (const el of currCells) {
  50. currColor.push(el.style.backgroundColor)
  51. el.style.backgroundColor = 'red'
  52. }
  53. }
  54. cell.onmouseout = function() {
  55. for (const el of currCells) {
  56. let color = currColor.shift()
  57. el.style.backgroundColor = color
  58. }
  59. }
  60. }
  61. }