🌱 → 🌳

[JavaScript] DOMκ³Ό DOM API λ³Έλ¬Έ

Client/Javascript

[JavaScript] DOMκ³Ό DOM API

BAY 2022. 7. 15. 17:06
728x90

πŸ“Œ DOM(Document Object Model)

 

πŸ“Œ DOM API

: Document Object Model Application Programming Interface

 

βœ… querySelector("μš”μ†Œ μ„ νƒμž")

CSS μš”μ†Œ μ„ νƒμžλ₯Ό μ‚¬μš©ν•΄μ„œ μžμ‹ μ΄ κ°€μ Έμ˜€κ³  μ‹Άμ–΄ν•˜λŠ” μš”μ†Œλ₯Ό κ°€μ Έμ˜€λŠ” λ©”μ†Œλ“œ

λ¬Έμ„œμ—μ„œ λ§Œλ‚˜λŠ” μ œμΌ 첫번째 μš”μ†Œλ₯Ό λ°˜ν™˜ν•¨

 

βœ… getElementById("id")

ID μ΄λ¦„을 λΆˆλŸ¬μ„œ ν•΄λ‹Ή IDλ₯Ό κ°€μ§€λŠ” μš”μ†Œλ₯Ό λΆˆλŸ¬μ˜€λŠ” λ©”μ†Œλ“œ

 

βœ… classList

classList.add('class1') : 선택 μš”μ†Œμ— class1μ΄λΌλŠ” class λ”ν•˜κΈ° 

classList.remove('class1') : 선택 μš”μ†Œμ— class1μ΄λΌλŠ” class 제거 

classList.contains('class1') : 선택 μš”μ†Œμ— class1μ΄λΌλŠ” class ν¬ν•¨ν•˜κ³  μžˆλŠ”μ§€ 확인

let boxEl = document.querySelector(".box");
boxEl.classList.add("active");
console.log(boxEl.classList.contains("active")); //true
boxEl.classList.remove("active");
console.log(boxEl.classList.contains("active")); //false

 

βœ… addEventListener(이벀트, λͺ…λ Ή)

선택 μš”μ†Œμ— μ§€μ •ν•œ μ΄λ²€νŠΈκ°€ λ°œμƒν•˜λ©΄, μ•½μ† 된 λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰μ‹œν‚€λŠ” λ©”μ†Œλ“œ

let boxEl = document.querySelector(".box");

boxEl.addEventListener("click", function () {
  alert("Add event listener");
});
// λ©”μ†Œλ“œ 체이닝 μ‚¬μš©
document.querySelector(".box").addEventListener("click", function () {
  alert("Add event listener");
});

βœ”οΈ addEventListener 이벀트 μ’…λ₯˜

 

  • click: 클릭
  • mouse 계열 
    • mouseover : μš”μ†Œμ— μ»€μ„œλ₯Ό μ˜¬λ Έμ„ λ•Œ 
    • mouseout : λ§ˆμš°μŠ€κ°€ μš”μ†Œλ₯Ό λ²—μ–΄λ‚  λ•Œ 
    • mousedown : 마우슀 λ²„νŠΌμ„ λˆ„λ₯΄κ³  μžˆλŠ” μƒνƒœ 
    • mouseup : 마우슀 λ²„νŠΌμ„ λ–ΌλŠ” μˆœκ°„ 
  • focus : ν¬μ»€μŠ€κ°€ 갔을 λ•Œ 
  • blur : ν¬μ»€μŠ€κ°€ λ²—μ–΄λ‚˜λŠ” μˆœκ°„
  • key 계열 
    • keypress : ν‚€λ₯Ό λˆ„λ₯΄λŠ” μˆœκ°„ + λˆ„λ₯΄κ³  μžˆλŠ” λ™μ•ˆ 계속 λ°œμƒ
    • keydown : ν‚€λ₯Ό λˆ„λ₯΄λŠ” μˆœκ°„μ—λ§Œ λ°œμƒ
    • keyup : ν‚€λ₯Ό λˆŒλ €λ‹€κ°€ λ–ΌλŠ” μˆœκ°„
  • load : μ›Ή νŽ˜μ΄μ§€μ— ν•„μš”ν•œ λͺ¨λ“  파일(HTML, CSS, JS λ“±)의 λ‹€μš΄λ‘œλ“œκ°€ μ™„λ£Œ λ˜μ—ˆμ„ λ•Œ
  • resize: λΈŒλΌμš°μ € 창의 크기가 λ³€κ²½ 될 λ•Œ 
  • scroll: 슀크둀이 λ°œμƒν•  λ•Œ 
  • unload: 링크λ₯Ό 타고 μ΄λ™ν•˜κ±°λ‚˜, λΈŒλΌμš°μ €λ₯Ό 닫을 λ•Œ
  • change: 폼 ν•„λ“œμ˜ μƒνƒœκ°€ λ³€κ²½ λ˜μ—ˆμ„ λ•Œ 

 

βœ… setAttribute("속성λͺ…", "지정할 속성")

HTML μš”μ†Œ 속성 μΆ”κ°€

μ„ νƒν•œ μš”μ†Œμ˜ 속성 값을 직접 지정할 수 μžˆλŠ” λ©”μ†Œλ“œ

let boxEl = document.querySelector(".box");
let inputEl = document.getElementById("input");
boxEl.addEventListener("click", function () {
  // inputEl.setAttribute("placeholder", "톡합검색");
  // inputEl.setAttribute("type", "button");
  inputEl.setAttribute("type", "password");
});

 

βœ… textContent

μ„ νƒν•œ μš”μ†Œμ˜ textcontent κ°’ λ³€κ²½ν•  수 있음

let boxEl = document.querySelector(".box");
boxEl.addEventListener("click", function () {
  boxEl.textContent = "hi";
});

 

βœ… querySelextorAll("μš”μ†Œ μ„ νƒμž")

λ¬Έμ„œμ— μ‘΄μž¬ν•˜λŠ” λͺ¨λ“  μš”μ†Œλ₯Ό μ°Ύμ•„μ£ΌλŠ” λ©”μ†Œλ“œ

λͺ¨λ“  μš”μ†Œλ₯Ό κ°€μ Έμ™€μ„œ λ°°μ—΄(같은) λ°μ΄ν„°λ‘œ λ§Œλ“€μ–΄ 쀌

 

728x90