ν‹°μŠ€ν† λ¦¬ λ·°

ν¬μŠ€νŒ… ν•œ 이유

μ…€λ ‰ν„°λ‘œ μš”μ†Œλ₯Ό 선택할 일이 μžˆμ—ˆλŠ”λ°
νƒμƒ‰λŒ€μƒμΈ 본인을 ν¬ν•¨ν•˜μ—¬ ν•˜μœ„λ‘œ νƒμƒ‰ν•˜κ³  μ‹Άμ—ˆλ‹€.

κ°€μž₯ 많이 μ“°μ΄λŠ” λ©”μ†Œλ“œλ“€μ„ μ‚΄νŽ΄λ³΄μ•˜λŠ”λ°,
querySelector/querySelectorAll 의 경우 본인을 μ œμ™Έν•˜κ³  ν•˜μœ„λ‘œ νƒμƒ‰ν•˜κ³ ,
closest λŠ” 본인을 ν¬ν•¨ν•˜μ—¬ μƒμœ„λ‘œ νƒμƒ‰ν•œλ‹€.

λ‘˜ λ‹€ λ§˜μ— μ•ˆλ“€μ—ˆλ‹€.

κ·Έλž˜μ„œ 결ꡭ은 matchesλ₯Ό μΆ”κ°€μ μœΌλ‘œ ν™œμš©ν•˜μ—¬ 선택할 수 μžˆλŠ” 방법을 μ°Ύμ•„λƒˆλ‹€.

(κ·Έ κ³Όμ •μ—μ„œ μ…€λ ‰ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ DOMμš”μ†Œλ₯Ό 선택할 수 μžˆλŠ” 방법듀을 μ£„λ‹€κ±°μ˜ ꡬ글링해봀기 λ•Œλ¬Έμ—. ν¬μŠ€νŒ…ν–ˆλ‹€λŠ” 뜻)

 

μ…€λ ‰ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ DOMμš”μ†Œλ₯Ό 선택할 수 μžˆλŠ” 방법듀

  1. element.querySelector(selector) & element.querySelectorAll(selector) -> μžμ‹μš”μ†Œμ€‘μ—μ„œ 탐색
  2. element.closest(selector) -> λ³ΈμΈμœΌλ‘œλΆ€ν„° μ‹œμž‘ν•˜μ—¬ (본인포함) μƒμœ„μš”μ†Œλ‘œ 탐색
  3. 직접 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œλŠ” μ•„λ‹ˆμ§€λ§Œ, element.matches(selector)λ₯Ό μ΄μš©ν•˜μ—¬ 탐색

 

1. querySelector & querySelectorAll

κ°€μž₯ 기본적으둜 querySelector, querySelectorAll 이 μžˆλ‹€.

element = baseElement.querySelector(selectors);
elementList = baseElement.querySelectorAll(selectors);

μ…€λ ‰ν„° ν˜Ήμ€ μ…€λ ‰ν„° λ­‰μΉ˜λ₯Ό μž…λ ₯ν•˜λ©΄, 각각 쑰건을 λ§Œμ‘±ν•˜λŠ” μš”μ†Œ, μš”μ†Œλ°°μ—΄μ„ λ°˜ν™˜ν•œλ‹€.

baseElement(본인)을 μ œμ™Έν•˜κ³  ν•˜μœ„μš”μ†Œλ“€μ„ νƒμƒ‰ν•œλ‹€.

 

2. closest

targetElement.closest(selectors);

μžμ‹ μœΌλ‘œλΆ€ν„° μΆœλ°œν•˜μ—¬ λΆ€λͺ¨μš”μ†Œλ‘œ ν–₯ν•œλ‹€.

μ§€μ •ν•œ μ„ νƒμžμ— λ§Œμ‘±ν•  λ•ŒκΉŒμ§€ νƒμƒ‰ν•œλ‹€.

 

3. element.matches(selector) λ₯Ό ν™œμš©ν•œ 방법

일단 matches μžμ²΄λŠ” Boolean 값을 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œμΈλ°

var result = element.matches(selectorString);

μ„ νƒμžλ₯Ό λ§Œμ‘±ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈλ©΄ true, 그렇지 μ•ŠμœΌλ©΄ false λ₯Ό λ°˜ν™˜ν•œλ‹€.

 


μ²˜μŒμ— κΆκΈˆν–ˆλ˜
νƒμƒ‰λŒ€μƒμΈ 본인을 ν¬ν•¨ν•˜μ—¬ ν•˜μœ„λ‘œ νƒμƒ‰ν•˜λŠ” 방법을 matchesλ₯Ό ν™œμš©ν•˜μ—¬ κ΅¬ν˜„ν•΄λ³΄μ•˜λ‹€.

 

    <div class="box carrot">
      <div class="one"></div>
      <div class="two carrot"></div>
      <div class="three carrot"></div>
    </div>

μœ„μ™€ 같은 HTML λ§ˆν¬μ—… μš”μ†Œλ“€μ΄ μžˆμ„ λ•Œ,
νƒμƒ‰λŒ€μƒμ„ box 둜 ν•˜κ³ , 본인을 ν¬ν•¨ν•˜μ—¬ carrot μ΄λΌλŠ” ν΄λž˜μŠ€μ΄λ¦„μ„ 가진 μš”μ†Œλ₯Ό νƒμƒ‰ν•˜κ³  μ‹Άλ‹€λ©΄?

 

querySelector 처럼 처음으둜 찾은 μš”μ†Œ ν•˜λ‚˜λ§Œμ„ λ°˜ν™˜ν•˜λ €λ©΄ μš”λ ‡κ²Œ μ“Έ 수 μžˆλ‹€.

element.matches(selector) && element || element.querySelector(selector);

 

μ‹€μ œλ‘œ μ μš©ν•œλ‹€λ©΄ μ•„λž˜μ™€ κ°™λ‹€.

 

λ‹€μŒμœΌλ‘œ querySelectorAll처럼 처음으둜 쑰건을 λ§Œμ‘±ν•˜λŠ” λͺ¨λ“  μš”μ†Œλ₯Ό λ°˜ν™˜ν•˜λ €λ©΄ μš”λ ‡κ²Œ μ“Έ 수 μžˆλ‹€.

[element, ...element.querySelectorAll(selector)].filter(elem => elem.matches(selector));

본인을 첫번째둜 λ„£κ³ , κ·Έ 뒀에 ν•˜μœ„μš”μ†Œλ“€μ„ querySelectorAll 둜 μ„ νƒν•œ λ’€ νŽΌμΉ˜κΈ°μ—°μ‚°μž(...)둜 μΆ”κ°€ν–ˆλ‹€.
그리고 κ·Έ λ°°μ—΄ μ•ˆμ—μ„œ ".carrot" 을 λ§Œμ‘±ν•˜λŠ” 녀석을 ν•„ν„°λ§ν•œλ‹€.

 

λ°˜ν™˜κ°’μ€ λ‹€μŒκ³Ό κ°™λ‹€.

querySelectorAll을 마치 본인을 ν¬ν•¨ν•˜μ—¬ νƒμƒ‰ν•œ 것 마λƒ₯ μ“Έ 수 μžˆλ‹€.

 

 

 

μ°Έκ³  :
https://developer.mozilla.org/ko/docs/Web/API/Element/querySelector
https://developer.mozilla.org/ko/docs/Web/API/Element/closest
https://developer.mozilla.org/en-US/docs/Web/API/Element/matches
https://stackoverflow.com/questions/60831411/how-to-have-element-queryselector-include-the-root-element/60831454#60831454

λŒ“κΈ€