정보/WEB

이벤트(addEventListener)

여기블로그 2022. 12. 25. 01:39
728x90
반응형
<!DOCTYPE html>
<html lang="ko">
    <head>
        <title>이벤트</title>
            <meta charset="UTF-8">
            <meta http-equiv="x-uv-compatible">
    </head>
    <body>
        <div id="button">Click!</div>
        <script>
            // 선택된 요소 .addEventListener(이벤트 종료, 함수)
            let buttonElement = window.document.getElementById('button');
            buttonElement.addEventListener('click', () => {
                alert('hello');
                // alert('hello');
                // alert(x); x라는 내용을 가지는 메세지 출력
                if (confirm('Really?')) {
                    alert('yes');
                    // confirm(x) x라는 내용을 가지는 메세지를 출력하나 [확인]클릭시 true, [취소]클릭시 false를 반환한다.
                     }
                else{
                    alert('no');
                }
            });


        </script>
    </body>
</html>
728x90
반응형