제어대상찾기(querySelector, querySelectorAll)
본문 바로가기

Frontend/dom

제어대상찾기(querySelector, querySelectorAll)

querySelector('li')는 첫번째 li만

querySelectorAll('li')은 모든 li태그선택

<!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>
    var lis = document.querySelectorAll('li');
    for(var name in lis){
        lis[name].style.color = 'blue';
    }
</script>
</body>
</html>

opentutorials.org/module/904/6656

 

제어 대상을 찾기 - 웹브라우저 JavaScript

문서를 자바스크립트로 제어하려면 제어의 대상에 해당되는 객체를 찾는 것이 제일 먼저 할 일이다. 문서 내에서 객체를 찾는 방법은 document 객체의 메소드를 이용한다.  document.getElementsByTagName

opentutorials.org

jquery에서는

$('li')하면 모든 li태그 선택

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
        crossorigin="anonymous"></script>
    <title>Document</title>

</head>
<body>
    <ul>
        <li>html</li>
        <li>css</li>
        <li>js</li>
    </ul>
    <ul>
        <ol>HTML</ol>
        <ol>CSS</ol>
        <ol>JS</ol>
    </ul>
        <script>
            var lis = $('li');
            lis.map(function (index, elem) {
                // console.log(index, elem)
                $(elem).css('color', 'red')
            });
        </script>
</body>
</html>

api.jquery.com/

 

jQuery API Documentation

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. If you're new t

api.jquery.com

 

 

제어 대상을 찾기 - 웹브라우저 JavaScript

문서를 자바스크립트로 제어하려면 제어의 대상에 해당되는 객체를 찾는 것이 제일 먼저 할 일이다. 문서 내에서 객체를 찾는 방법은 document 객체의 메소드를 이용한다.  document.getElementsByTagName

opentutorials.org

 

반응형