자바스크립트 함수호출

이세라·2024년 10월 1일
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>aaa</title>
    <script>

//방법1        
        document.addEventListener("DOMContentLoaded",function(){
          //const h1 = document.querySelector("h1");//h1 태그 불러오기
            const h1 = document.querySelector("#box");//id가 box인 요소 찾아 h1변수에 담기
            const btn = document.querySelector(".btn");
          //const but = document.querySelector("[type=button]");
            btn.addEventListener("click",function(){
                h1.textContent="즐거운 시간!!";
                clickTest(); //함수 호출
            })
        });

        function clickTest(){
            alert("Hello javascript");
        }

/*         

//방법2
        document.addEventListener("DOMContentLoaded",function(){
          //const h1 = document.querySelector("h1");//h1 태그 불러오기
            const h1 = document.querySelector("#box");//id가 box인 요소 찾아 h1변수에 담기
            const btn = document.querySelector(".btn");
          //const but = document.querySelector("[type=button]");
            btn.addEventListener("click",function(){
                h1.textContent="즐거운 시간!!";
                alert("Hello javascript");
            })
        });

 */

    </script>
</head>
<body>
    <h1 id="box"></h1>
    <!-- <button type="button" class="btn">버튼 클릭</button> --> 
    <button type="button" class="btn">버튼 클릭</button>

</body>
</html>
profile
개린이입니다...

0개의 댓글