index.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" href="style.css">
  8. <title>Document</title>
  9. </head>
  10. <body>
  11. <script>
  12. {
  13. let table = document.createElement('table')
  14. let tr1 = document.createElement('tr')
  15. table.append(tr1)
  16. tr1.innerHTML = '<th>0</th>'
  17. function backgroundColored(event) {
  18. let arrClasses = event.target.classList
  19. for (elemClass of arrClasses) {
  20. let elements = document.querySelectorAll('.' + elemClass)
  21. for (element of elements) {
  22. element.classList.add('backgroundColorGrey')
  23. }
  24. }
  25. }
  26. function backgroundDefault(event) {
  27. let arrClasses = event.target.classList
  28. for (elemClass of arrClasses) {
  29. let elements = document.querySelectorAll('.' + elemClass)
  30. for (element of elements) {
  31. element.classList.remove('backgroundColorGrey')
  32. }
  33. }
  34. }
  35. for (n = 1; n < 10; n++) {
  36. let tr = document.createElement('tr')
  37. table.append(tr)
  38. let td1 = document.createElement('td')
  39. tr.append(td1)
  40. td1.innerText = n
  41. for (i = 1; i < 10; i++) {
  42. let td = document.createElement('td')
  43. tr.append(td)
  44. td.innerText = n * i
  45. td.classList.add('row' + n, 'col' + i)
  46. td.onmouseover = backgroundColored
  47. td.onmouseout = backgroundDefault
  48. if (n === 1) {
  49. let th = document.createElement('th')
  50. th.innerText = n * i
  51. tr1.append(th)
  52. }
  53. }
  54. }
  55. document.body.append(table)
  56. }
  57. </script>
  58. </body>
  59. </html>