p 688

수빈·2023년 2월 20일
0
<!DOCTYPE html>
<html>
    <body>
        <ul>
            <li id="apple">Apple</li>
            <li id="banana">Banana</li>
            <li id="orange">Oranbe</li>
        </ul>
        <script>
            const $elems = document.getElementsByTagName('li');

            for(const elem of $elems) {
                elem.style.color = 'red';
            }
          
            // [...$elems].forEach(v => v.style.color ='red');
            // HTMLCollection 객체는 유사 배열 객체 & 이터러블이므로
            // 스프레드 문법으로 [배열]로 바꿔야 한다.
        </script>
    </body>
</html>

10

0개의 댓글