-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (56 loc) · 1.85 KB
/
app.js
File metadata and controls
71 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const heading = document.getElementById('hello')
// const heading2 = document.getElementsByTagName('h2')[0]
// const heading2 = document.getElementsByClassName('h2-class')[0]
// const heading2 = document.querySelector('.h2-class')
// const heading2 = document.querySelector('#sub-hello') // Всегда 1 элемент
const heading2 = document.querySelector('h2')
// const heading3 = heading2.nextElementSibling
const h2List = document.querySelectorAll('h2')
const heading3 = h2List[h2List.length - 1]
setTimeout(() => {
addStylesTo(heading, 'JavaScript')
}, 1500)
const link = heading3.querySelector('a')
link.addEventListener('click', (event) => {
event.preventDefault()
console.log('Click on link', event.target.getAttribute('href'))
const url = event.target.getAttribute('href')
window.location = url
})
setTimeout(() => {
addStylesTo(link, 'Практикуйся', 'blue')
}, 3000)
setTimeout(() => {
addStylesTo(heading2, 'И все получится!', 'yellow', '2rem')
}, 4500)
function addStylesTo(node, text, color = 'red', fontSize) {
node.textContent = text
node.style.color = color
node.style.textAlign = 'center'
node.style.backgroundColor = 'black'
node.style.padding = '2rem'
node.style.display = 'block'
node.style.width = '100%'
// falsy: '', undefined, null, 0, false
if (fontSize) {
node.style.fontSize = fontSize
}
}
heading.onclick = () => {
if (heading.style.color === 'red') {
heading.style.color = '#000'
heading.style.backgroundColor = '#fff'
} else {
heading.style.color = 'red'
heading.style.backgroundColor = '#000'
}
}
heading2.addEventListener('dblclick', () => {
if (heading2.style.color === 'yellow') {
heading2.style.color = '#000'
heading2.style.backgroundColor = '#fff'
} else {
heading2.style.color = 'yellow'
heading2.style.backgroundColor = '#000'
}
})