Skip to content

Commit 69faffd

Browse files
committed
[문서] 1.4 getElement*, querySelector*로 요소 검색하기 병합충돌 해결
1 parent 29f79b0 commit 69faffd

1 file changed

Lines changed: 0 additions & 27 deletions

File tree

  • 2-ui/1-document/04-searching-elements-dom

2-ui/1-document/04-searching-elements-dom/article.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,8 @@
5454
</script>
5555
```
5656

57-
<<<<<<< HEAD
5857
```warn header="id를 따서 만들어진 전역변수를 요소 접근 시 사용하지 마세요."
5958
`id`에 대응하는 전역변수는 [명세서](http://www.whatwg.org/specs/web-apps/current-work/#dom-window-nameditem)의 내용을 구현해 만들어진 것으로 표준이긴 하지만 하위 호환성을 위해 남겨둔 동작입니다.
60-
=======
61-
```warn header="Please don't use id-named global variables to access elements"
62-
This behavior is described [in the specification](https://html.spec.whatwg.org/multipage/window-object.html#named-access-on-the-window-object), but it is supported mainly for compatibility.
63-
>>>>>>> upstream/master
6459
6560
브라우저는 스크립트의 네임스페이스와 DOM의 네임스페이스를 함께 사용할 수 있도록 해서 개발자의 편의를 도모합니다. 그런데 이런 방식은 스크립트가 간단할 땐 괜찮지만, 이름이 충돌할 가능성이 있기 때문에 추천하는 방식은 아닙니다. HTML을 보지 않은 상황에서 코드만 보고 변수의 출처를 알기 힘들다는 단점도 있습니다.
6661
@@ -75,13 +70,8 @@ This behavior is described [in the specification](https://html.spec.whatwg.org/m
7570
같은 `id`를 가진 요소가 여러 개 있으면 `document.getElementById`같이 `id`를 이용해 요소를 검색하는 메서드의 동작이 예측 불가능해집니다. 검색된 여러 요소 중 어떤 요소를 반환할지 판단하지 못해 임의의 요소가 반환되죠. 문서 내 동일 `id`가 없도록 해 이런 일을 방지하도록 합시다.
7671
```
7772

78-
<<<<<<< HEAD
7973
```warn header="`anyNode.getElementById`가 아닌 `document.getElementById`"
8074
`getElementById``document` 객체를 대상으로 해당 `id`를 가진 요소 노드를 찾아 줍니다. 문서 노드가 아닌 다른 노드엔 호출할 수 없습니다.
81-
=======
82-
```warn header="Only `document.getElementById`, not `anyElem.getElementById`"
83-
The method `getElementById` can be called only on `document` object. It looks for the given `id` in the whole document.
84-
>>>>>>> upstream/master
8575
```
8676
8777
## querySelectorAll [#querySelectorAll]
@@ -126,11 +116,7 @@ querySelectorAll에는 `:hover`나 `:active` 같은 CSS 선택자의 가상 클
126116

127117
지금까지 소개한 모든 메서드는 DOM 검색에 쓰입니다.
128118

129-
<<<<<<< HEAD
130119
[elem.matches(css)](http://dom.spec.whatwg.org/#dom-element-matches)는 DOM을 검색하는 일이 아닌 조금 다른 일을 합니다. 이 메서드는 요소 `elem`이 주어진 CSS 선택자와 일치하는지 여부를 판단해줍니다. 일치한다면 `true`, 아니라면 `false`를 반환하죠.
131-
=======
132-
The [elem.matches(css)](https://dom.spec.whatwg.org/#dom-element-matches) does not look for anything, it merely checks if `elem` matches the given CSS-selector. It returns `true` or `false`.
133-
>>>>>>> upstream/master
134120

135121
요소가 담겨있는 배열 등을 순회해 원하는 요소만 걸러내고자 할 때 유용합니다.
136122

@@ -156,11 +142,7 @@ The [elem.matches(css)](https://dom.spec.whatwg.org/#dom-element-matches) does n
156142

157143
부모 요소, 부모 요소의 부모 요소 등 DOM 트리에서 특정 요소의 상위에 있는 요소들은 *조상(ancestor)* 요소라고 합니다.
158144

159-
<<<<<<< HEAD
160145
메서드 `elem.closest(css)``elem` 자기 자신을 포함하여 CSS 선택자와 일치하는 가장 가까운 조상 요소를 찾을 수 있게 도와줍니다.
161-
=======
162-
The method `elem.closest(css)` looks for the nearest ancestor that matches the CSS-selector. The `elem` itself is also included in the search.
163-
>>>>>>> upstream/master
164146

165147
`closest`메서드는 해당 요소부터 시작해 DOM 트리를 한 단계씩 거슬러 올라가면서 원하는 요소를 찾습니다. CSS 선택자와 일치하는 요소를 찾으면, 검색을 중단하고 해당 요소를 반환합니다.
166148

@@ -171,13 +153,8 @@ The method `elem.closest(css)` looks for the nearest ancestor that matches the C
171153

172154
<div class="contents">
173155
<ul class="book">
174-
<<<<<<< HEAD
175156
<li class="chapter">1장</li>
176157
<li class="chapter">2장</li>
177-
=======
178-
<li class="chapter">Chapter 1</li>
179-
<li class="chapter">Chapter 2</li>
180-
>>>>>>> upstream/master
181158
</ul>
182159
</div>
183160

@@ -386,11 +363,7 @@ DOM에서 원하는 노드를 검색하게 해주는 주요 메서드 6가지는
386363
</tbody>
387364
</table>
388365
389-
<<<<<<< HEAD
390366
아마 실무에선 `querySelector`나 `querySelectorAll`을 가장 많이 사용하실 겁니다. `getElementBy`로 시작하는 메서드는 대개 오래된 스크립트에서 만날 수 있는데, 일부 이 메서드가 꼭 필요한 상황에서 쓰이는 경우도 있습니다.
391-
=======
392-
By far the most used are `querySelector` and `querySelectorAll`, but `getElement(s)By*` can be sporadically helpful or found in the old scripts.
393-
>>>>>>> upstream/master
394367
395368
이 외에 알아두면 좋을 만한 메서드는 아래와 같습니다.
396369

0 commit comments

Comments
 (0)