Js-HW7.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. let $table = document.createElement('table');
  2. function rerenderColor(e, color) {
  3. Array.from($table.children).map(item => {
  4. Array.from(item.children).filter(item => item.cellIndex === e.target.cellIndex).map(item => item.style.backgroundColor = color)
  5. })
  6. }
  7. for(let i = 1; i < 10; i++) {
  8. let $tr = document.createElement('tr')
  9. for(let j = 1; j < 10; j++) {
  10. // $td.addEventListener('mouseover', (e) => rerenderColor(e, 'blue'))
  11. // $td.addEventListener('mouseout', (e) => rerenderColor(e, 'transparent'))
  12. let $td = document.createElement('td')
  13. $td.innerText = String(j * i)
  14. $td.addEventListener('mouseover', (e) => {
  15. rerenderColor(e, 'red')
  16. $td.style.backgroundColor = 'green'
  17. })
  18. $td.addEventListener('mouseout', (e) => rerenderColor(e, 'transparent'))
  19. $tr.appendChild($td)
  20. }
  21. $table.appendChild($tr)
  22. }
  23. document.body.appendChild($table)
  24. const $n1 = document.querySelector('#n1')
  25. const $n2 = document.querySelector('#n2')
  26. const $calcBtn = document.querySelector('#calcBtn')
  27. const $result = document.querySelector('#result')
  28. const calcSum = () => {
  29. return $result.innerText = String(+$n1.value + +$n2.value)
  30. }
  31. $calcBtn.addEventListener( 'click', () => {
  32. calcSum()
  33. })
  34. $n1.onchange = calcSum()
  35. $n2.onchange = calcSum()
  36. $calcBtn.onclick = calcSum()