Advanced_Dom

Advanced Dom

How the Dom really works?

![BB93EF78E760EDFA026A6E09647E68DA](C:\Users\name\Documents\Tencent Files\747417582\FileRecv\MobileFile\BB93EF78E760EDFA026A6E09647E68DA.png)

Selecting, Creating, and Deleting Elements

Select

1
2
3
4
5
6
7
const queryAllButtons = document.querySelectorAll('button');
console.log(queryAllButtons); // NodeList(9)
// It is fixed

const getAllButtons = document.getElementsByTagName('button');
console.log(getAllButtons); // HTMLCollection(9)
// It updates itself as the html changes.

Create

1
2
3
4
5
6
7
8
9
10
11
12
13
const header = document.querySelector('.header');
const message = document.createElement('div');
message.classList.add('cookie-message');
message.textContent =
'We use cookied for improved functionality and analytics.';
message.innerHTML =
'We use cookied for improved functionality and analytics.<button class="btn btn--close-cookie">Got it!</button>';
header.prepend(message);
header.append(message);

header.after(message);
header.before(message);
message.remove();

Styles, Attributes and Classes

Styles

1
2
3
message.style.width = '120%';
console.log(message.style.width); // 120%
console.log(message.style.height); // nothing

Only works for the inline style.

1
2
console.log(getComputedStyle(message)); // CSSStyleDeclaration
console.log(getComputedStyle(message).height); // 50.6px

Change the css variables

1
document.documentElement.style.setProperty('--color-primary', 'red')

Attributes

1
2
3
4
5
6
7
8
const logo = document.querySelector('.nav__logo');
console.log(logo.alt); // Bankist logo
console.log(logo.src); // http://127.0.0.1:5501/complete-javascript-course/13-Advanced-DOM-Bankist/starter/img/logo.png
console.log(logo.getAttribute('src')); // img/logo.png
console.log(logo.className); // nav__logo

logo.setAttribute('designer', 'jonas');
console.log(logo.getAttribute('designer')); // jonas

Data attributes

1
logo.dataset

Classes

1
2
3
4
logo.classList.add('c');
logo.classList.remove('c');
logo.classList.toggle('c');
logo.classList.contains('c');

Implementing Smooth Scrolling

Old school way

1
2
3
4
5
6
7
8
9
10
11
const btnScrollTo = document.querySelector('.btn--scroll-to');
const section1 = document.querySelector('#section--1');

btnScrollTo.addEventListener('click', e => {
const s1croods = section1.getBoundingClientRect();
window.scrollTo({
left: s1croods.left + window.pageXOffset,
top: s1croods.top + window.pageYOffset,
behavior: 'smooth',
});
});

Modern way

1
2
3
4
5
6
const btnScrollTo = document.querySelector('.btn--scroll-to');
const section1 = document.querySelector('#section--1');

btnScrollTo.addEventListener('click', e => {
section1.scrollIntoView({ behavior: 'smooth' });
});

Type of Events and Event Handlers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const h1 = document.querySelector('h1');
const alert = e => {
console.log('Enter');

h1.removeEventListener('mouseenter', alert);
};
h1.addEventListener('mouseenter', alert);
h1.addEventListener('mouseleave', e => {
console.log('Leave');
});

window.addEventListener('keydown', e => {
console.log(e);
});

Event Propagation_ Bubbling and Capturing

Event Delegation

Dom Traversing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const h1 = document.querySelector('h1');

//Going downwards: child
console.log(h1.querySelectorAll('.highlight'));
console.log(h1.childNodes); // NodeList(9)
console.log(h1.children); // HTMLCollection(3)
h1.firstElementChild.style.color = 'white';
h1.lastElementChild.style.color = 'white';

// Going upwards: parents
console.log(h1.parentNode);
console.log(h1.parentElement);
h1.closest('.header').style.background = 'var(--gradient-secondary)';

// Going sideways: siblings
console.log(h1.previousElementSibling);
console.log(h1.nextElementSibling);

console.log(h1.parentElement.children);

The Intersection Observer API

。。。。


本站由 @Eureka 使用 Stellar 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。