👋 Hello, I am Seung-ho Ham

🧑🏻‍💻 A Developers who create code that changes the world

카테고리 없음

[DEV] DOM API 2

Potato_H 2021. 6. 28. 08:26
반응형

- DOM API (Document Object Model, Application Programming Interface)


// HTML 요소(Element) 모두 검색/찾기
const boxEls = document.querySelectorAll('.box');
console.log(boxEls);

// 찾은 요소를 반복해서 함수 실행
// 익명 함수를 인수로 추가
boxEls.forEach(function () {});

//첫 번째 매개변수 (boxEl) : 반복 중인 요소
//두 번째 매개변수 (index) : 반복 중인 번호

boxEl.forEach(function (boxEl, index) {});

// 출력
boxEls.forEach(function (boxEl, index) {
    boxEl.classList.add('order-${index + 1}');
    console.log(index, boxEl);
});


const boxEl = document.querySelector('.box');

 

// Getter, 값을 얻는 용도

console.log(boxEl.textContent); // Box

 

// Setter, 값을 지정하는 용도

boxEl.textContent = 'abc';

console.log(boxEl.textContent); //abc