본문 바로가기
Javascript/이론

객체/for in 반복문/charAt/substring/replace/window.onload/querySelector(All)/getElementById

by SEOKIHOUSE 2023. 5. 26.
  • 객체 -->    {   ,   ,    ,} 이름을 가지고 있음
  • 변수명.제품명
  • 변수명[제품명]


  • for in


eat 익명함수처럼 쓴겅미

 


  • charAt : i번째 인덱스 출력
  • substring : 시작인덱스, 얻고싶은 인덱스 +1
  • replace: 값 교체


 


 

 


  • 익명함수는 호이스팅이 안된다!!

1)익명함수 아닐 경우

2)익명함수 인 경우


  • 실행순서에 따른 객체사용 오류 
  • querySelector(div, h1 등등을 가져온다) -->선택자로 1개선택


  • 해결방법 1)

h1이 없어서 실행이 안된다

h1이랑 h2 밑으로 내려보면 된다

 


  • 해결방법 2)
  • 흠 그럼... 스크립트를 올려서 하려면 window.onload = function()를 써보자 이게 

머선뜻잉냐?  문서가 실행한 뒤에 실행하라는 뜻

 


  • document.getElementById --> 해당 element의 id를 넣음으로 사용(id라고 정한걸 가져온다)
  • innerHTML 내부내용 변경

 


  • querySelectorAll ( 전부다 선택해서 배열처럼 사용가능)
  • id쓰려면앞에 #써야한다 class는 . 


  • 버튼누르면 배경색 변경되게 해보기(script위치 잘보기)
<!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">
    <title>Document</title>
    <script>
        window.onload = function() {
            //id값에 header인 놈을 찾아
            let header = document.getElementById('header');

            header.style.color = 'orange';
            header.style.background = 'red';
            header.innerHTML = 'From JavaScript';

            
        };
        function f() {
            document.querySelector('h1').style.backgroundColor ="skyblue";
        }
        </script>
</head>
<body>
    <h1 id="header">Header</h1>
    <h1>ppp</h1>
    <h2>zzzzz</h2>
    <button onclick="f()">RUN</button>
    <!-- <script>
        function f() {
            document.querySelector('h1').style.backgroundColor ="skyblue";
        }
    </script> -->
</body>
</html>