Advertisement
Hasli4

EventAnswer

Jun 10th, 2025
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1. Меняем фон по клику
  2. const bgBtn = document.querySelector('#bg-btn');
  3. bgBtn.addEventListener('click', function() {
  4.   document.body.style.backgroundColor = 'lightgreen';
  5. });
  6.  
  7. // 2. Текст при наведении и уходе
  8. const tip = document.querySelector('#tip');
  9. const originalText = tip.textContent;
  10. tip.addEventListener('mouseover', function() {
  11.   tip.textContent = 'Спасибо за внимание!';
  12. });
  13. tip.addEventListener('mouseout', function() {
  14.   tip.textContent = originalText;
  15. });
  16.  
  17. // 3. Увеличиваем текст двойным щелчком
  18. const textPara = document.querySelector('#text');
  19. textPara.addEventListener('dblclick', function() {
  20.   // получаем текущий размер или задаём 16px по умолчанию
  21.   const currSize = parseInt(window.getComputedStyle(textPara).fontSize, 10) || 16;
  22.   textPara.style.fontSize = (currSize + 4) + 'px';
  23. });
  24.  
  25. // 4. Показ вводимого текста
  26. const liveInput = document.querySelector('#live');
  27. const outputSpan = document.querySelector('#output');
  28. liveInput.addEventListener('input', function(event) {
  29.   outputSpan.textContent = event.target.value;
  30. });
  31.  
  32. // 5. Выбор из списка с alert
  33. const sel = document.querySelector('#sel');
  34. sel.addEventListener('change', function() {
  35.   alert('Вы выбрали: ' + sel.value);
  36. });
  37.  
  38. // 6. Сообщение при прокрутке
  39. const scrollBlock = document.querySelector('#scroll');
  40. scrollBlock.addEventListener('scroll', function() {
  41.   console.log('Прокрутка идёт');
  42. });
  43.  
  44. // 7. Нажатие Enter в поле
  45. const inp = document.querySelector('#inp');
  46. inp.addEventListener('keydown', function(event) {
  47.   if (event.key === 'Enter') {
  48.     console.log('Enter нажата');
  49.   }
  50. });
  51.  
  52. // 8. Включить/выключить клик на box2
  53. const onBtn = document.querySelector('#on');
  54. const offBtn = document.querySelector('#off');
  55. const box2 = document.querySelector('#box2');
  56.  
  57. function box2ClickHandler() {
  58.   box2.style.backgroundColor = box2.style.backgroundColor === 'yellow' ? '' : 'yellow';
  59. }
  60.  
  61. onBtn.addEventListener('click', function() {
  62.   box2.addEventListener('click', box2ClickHandler);
  63. });
  64. offBtn.addEventListener('click', function() {
  65.   box2.removeEventListener('click', box2ClickHandler);
  66. });
  67.  
  68. // 9. Меняем фон только у кликнутого пункта
  69. const items = document.querySelectorAll('#list li');
  70. items.forEach(function(li) {
  71.   li.addEventListener('click', function() {
  72.     li.style.backgroundColor = 'lightblue';
  73.   });
  74. });
  75.  
  76. // 10. Мигание кнопки с остановкой через 5 секунд
  77. const flashBtn = document.querySelector('#flash');
  78. flashBtn.addEventListener('click', function() {
  79.   let on = false;
  80.   const intervalId = setInterval(function() {
  81.     flashBtn.style.backgroundColor = on ? '' : 'red';
  82.     on = !on;
  83.   }, 1000);
  84.   setTimeout(function() {
  85.     clearInterval(intervalId);
  86.     flashBtn.style.backgroundColor = ''; // возвращаем исходный
  87.   }, 5000);
  88. });
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement