|
| 1 | +const heading = document.getElementById('hey') |
| 2 | +// const heading2 = document.getElementsByTagName('h2')[0] |
| 3 | +// const heading2 = document.getElementsByClassName('h2-class')[0] |
| 4 | +// const heading2 = document.querySelector('.h2-class') |
| 5 | +const heading2 = document.querySelector('h2') // Всегда возвращает один 1 попавшийся элемент |
| 6 | +// console.dir(heading2) |
| 7 | +// const heading3 = heading2.nextElementSibling |
| 8 | +const h2List = document.querySelectorAll('h2') |
| 9 | +// console.log(h2List) |
| 10 | +const heading3 = h2List[1] |
| 11 | +// console.log(heading3) |
| 12 | + |
| 13 | + |
| 14 | +// console.log(heading2) |
| 15 | + |
| 16 | + |
| 17 | +// console.dir(heading.textContent) |
| 18 | + |
| 19 | +setTimeout(() => { |
| 20 | + addStylesTo(heading, 'JavaScript') |
| 21 | +}, 2000) |
| 22 | + |
| 23 | +setTimeout(() => { |
| 24 | + addStylesTo(heading2, 'Is my favorite Programming Language!', 'blue') |
| 25 | +}, 3000) |
| 26 | + |
| 27 | +const link = heading3.querySelector('a') |
| 28 | +link.addEventListener('click', (event) => { |
| 29 | + event.preventDefault() |
| 30 | + console.log('Click on link', event.target.getAttribute('href')) |
| 31 | + url = event.target.getAttribute('href') |
| 32 | + |
| 33 | + window.location = url |
| 34 | +}) |
| 35 | +setTimeout(() => { |
| 36 | + addStylesTo(link, 'And I love it so much :)', 'orange', '3rem') |
| 37 | +}, 4000) |
| 38 | + |
| 39 | +function addStylesTo(node, text, color = 'red', fontSize) { |
| 40 | + node.textContent = text |
| 41 | + node.style.color = color |
| 42 | + node.style.textAlign = 'center' |
| 43 | + node.style.backgroundColor = 'black' |
| 44 | + node.style.padding = '2rem' |
| 45 | + node.style.display = 'block' |
| 46 | + node.style.width = '100%' |
| 47 | + // falsy: '', undefined, null, 0, false |
| 48 | + if (fontSize) { |
| 49 | + node.style.fontSize = fontSize |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// https://developer.mozilla.org/ru/docs/Web/Events |
| 54 | + |
| 55 | +heading.onclick = () => { |
| 56 | + if (heading.style.color === 'red') { |
| 57 | + heading.style.color = '#000' |
| 58 | + heading.style.backgroundColor = '#fff' |
| 59 | + } else { |
| 60 | + heading.style.color = 'red' |
| 61 | + heading.style.backgroundColor = '#000' |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +heading2.addEventListener('click', () => { |
| 66 | + if (heading2.style.color === 'blue') { |
| 67 | + heading2.style.color = '#000' |
| 68 | + heading2.style.backgroundColor = '#fff' |
| 69 | + } else { |
| 70 | + heading2.style.color = 'blue' |
| 71 | + heading2.style.backgroundColor = '#000' |
| 72 | + } |
| 73 | +}) |
| 74 | + |
0 commit comments