> For the complete documentation index, see [llms.txt](https://westsilver.gitbook.io/study-script/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://westsilver.gitbook.io/study-script/javascript/web-browser/untitled/undefined.md).

# 제어 대상 찾기

### document.getElementsByTagName

: 인자로 전달된 **태그명**에 해당하는 객체들을 찾아서 그 리스트를 [NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList)라는 유사 배열에 담아서 반환한다.

<div align="center"><figure><img src="/files/aJ1vBpEFXJgrw1rK96qM" alt=""><figcaption></figcaption></figure></div>

{% code lineNumbers="true" %}

```html
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<script>
    let lis = document.getElementsByTagName('li');
    for(let i=0; i < lis.length; i++){
        lis[i].style.color='red';   
    }
</script>
</body>
</html>
```

{% endcode %}

{% code lineNumbers="true" %}

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ol>
<script>
    let ul = document.getElementsByTagName('ul')[0];
    let lis = ul.getElementsByTagName('li');
    for(let i=0; lis.length; i++){
        lis[i].style.color='red';   
    }
</script>
</body>
</html>
```

{% endcode %}

### document.getElementsByClassName

: **class 속성의 값**을 기준으로 객체를 조회한다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li class="active">JavaScript</li>
</ul>
<script>
    let lis = document.getElementsByClassName('active');
    for(let i=0; i < lis.length; i++){
        lis[i].style.color='red';   
    }
</script>
</body>
</html>
```

### document.getElementById

: **id 값**을 기준으로 객체를 조회한다. 성능면에서 가장 우수하다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li id="active">CSS</li>
    <li>JavaScript</li>
</ul>
<script>
    let li = document.getElementById('active');
    li.style.color='red';
</script>
</body>
</html>
```

### document.querySelector

: **css 선택자의 문법을 이용해서 객체를 조회**할수도 있다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li>JavaScript</li>
</ol>
 
<script>
    let li = document.querySelector('li');
    li.style.color='red';
    let li = document.querySelector('.active');
    li.style.color='blue';
</script>
</body>
</html>
```

### document.querySelectorAll

: querySelector과 기본적인 동작방법은 같지만 **모든 객체를 조회**한다는 점이 다르다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li>JavaScript</li>
</ol>
 
<script>
    let lis = document.querySelectorAll('li');
    for(let name in lis){
        lis[name].style.color = 'blue';
    }
</script>
</body>
</html>
```
