script.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. let table = document.createElement('table')
  2. table.id = 'mulTable'
  3. for(let i = 1; i < 10; i++) {
  4. let tr = document.createElement('tr')
  5. for(let j = 1; j < 10; j++) {
  6. let td = document.createElement('td')
  7. td.style.textAlign = 'center'
  8. td.style.padding = '5px'
  9. td.style.border = '1px solid dimgrey'
  10. td.innerText = `${i * j}`
  11. td_tr_Listener(td, j, i, tr)
  12. tr.appendChild(td)
  13. }
  14. table.appendChild(tr)
  15. }
  16. document.getElementsByTagName('body')[0].appendChild(table)
  17. function td_tr_Listener(td, col, row, tr) {
  18. let _td = td
  19. let _tr = tr
  20. let _col, _row
  21. td.onmouseover = function(td) {
  22. _col = col - 1
  23. _row = row - 1
  24. for(let i of _tr.parentElement.children) {
  25. i.children[_col].style.backgroundColor = 'black'
  26. i.children[_col].style.color = 'white'
  27. }
  28. _td.style.backgroundColor = 'magenta'
  29. _tr.parentElement.children[_row].style.backgroundColor = 'aquamarine'
  30. }
  31. td.onmouseout = function(td) {
  32. for(let i of _tr.parentElement.children) {
  33. for(let j of i.children) {
  34. j.style.backgroundColor = 'transparent'
  35. j.style.color = 'black'
  36. }
  37. }
  38. _td.style.backgroundColor = 'transparent'
  39. _tr.style.backgroundColor = 'transparent'
  40. }
  41. }