logo

예제가 포함된 JavaScript addEventListener()

그만큼 addEventListener() 메서드 EventTarget 인터페이스는 지정된 이벤트가 대상에 전달될 때마다 호출되는 함수를 설정합니다. 이 방법을 사용하면 요소에 여러 이벤트 핸들러를 허용하여 웹 애플리케이션 내에서 동적이고 유연한 상호 작용 관리를 가능하게 합니다.

통사론:



element.addEventListener(event, listener, useCapture);>

매개변수:

  • 이벤트: 이벤트는 유효한 JavaScript 이벤트일 수 있습니다. 이벤트는 onclick 대신 click을 사용하거나 onmousedown 대신 mousedown을 사용하는 것과 같이 접두사 없이 사용됩니다.
  • 리스너(핸들러 함수): 발생하는 이벤트에 응답하는 JavaScript 함수일 수 있습니다.
  • 사용캡처: 이벤트 전파를 제어하는 ​​데 사용되는 선택적 매개변수입니다. 부울 값이 전달됩니다. 진실 캡처 단계를 나타냅니다. 거짓 버블링 단계를 나타냅니다.

예시 1: 이 예에서는 버튼을 클릭하면 웹페이지에 텍스트가 표시됩니다.

HTML
     문서제목> 머리> <body>  <button id='try'>여기를 클릭하세요버튼><h1 id='text'>h1><script>document.getElementById('try').addEventListener('click', function () { document.getElementById('text').innerText = 'techcodeview.com'; });  스크립트> 본문> html>></pre> </code> <p dir='ltr'>  <b>  <strong>산출:</strong>  </b>  <span> </span></p>  <img src='//techcodeview.com/img/javascript-methods/82/javascript-addeventlistener-with-examples.webp' alt="예제가 포함된 JavaScript addEventListener()"><p>예제가 포함된 JavaScript addEventListener()</p> <br /> <div id="container-4ae8242bd8e42d94c5c0bf390d5144e6"></div><br /> <p dir='ltr'>  <br>  <b>  <strong>예 2:</strong>  </b>  <span>이 예에서는 mouseover 및 mouseout 두 이벤트가 동일한 요소에 추가됩니다. 텍스트 위에 마우스를 올리면 mouseover 이벤트가 발생하고 RespondMouseOver 함수가 호출됩니다. 마찬가지로 mouseout 이벤트의 경우 RespondMouseOut 함수가 호출됩니다.</span></p>HTML<code class='hljs'> <pre class='hljs'> <html lang='en'> <head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width,  initial-scale=1.0'> <title>문서제목> 머리> <body>  <button id='clickIt'>여기를 클릭하세요버튼><p id='hoverPara'>이 텍스트 위로 마우스를 가져가세요 !p> <b id='effect'>비><script>const x = document.getElementById('clickIt');  const y = document.getElementById('hoverPara');  x.addEventListener('click', RespondClick);  y.addEventListener('mouseover', RespondMouseOver);  y.addEventListener('mouseout', RespondMouseOut);  function RespondMouseOver() { document.getElementById('효과').innerHTML += 'MouseOver 이벤트' + ' ';  } function RespondMouseOut() { document.getElementById('효과').innerHTML += 'MouseOut 이벤트' + ' ';  } function RespondClick() { document.getElementById('효과').innerHTML += '클릭 이벤트' + ' ';  } 스크립트> 본문> html>></pre> </code> <p dir='ltr'>  <b>  <strong>산출:</strong>  </b>  <span> </span></p>  <img src='//techcodeview.com/img/javascript-methods/82/javascript-addeventlistener-with-examples-2.webp' alt="예제가 포함된 JavaScript addEventListener()"><p>예제가 포함된 JavaScript addEventListener()</p> <br /> <div id="container-4ae8242bd8e42d94c5c0bf390d5144e6"></div><br />  <br>  <br></article>
                         
                        </div><!--//content-->
                    </div><!--//section-inner-->                 
                </section><!--//section-->
    
            </div><!--//primary-->
            <div class="secondary col-md-4 col-sm-12 col-xs-12">
                  <aside class="info aside section">
                    <div class="section-inner">
                        <h2 class="">범주</h2>
                        <div class="content">
                            <ul class="list-unstyled">
                                <li> <a href="/arrays/">배열</a> </li><li> <a href="/information-security/">정보 보안</a> </li><li> <a href="/typescript-tutorial/">타입스크립트 튜토리얼</a> </li><li> <a href="/python-library/">Python 라이브러리</a> </li><li> <a href="/python-file-handling-programs/">Python 파일 처리 프로그램</a> </li><li> <a href="/commerce-12th/">커머스 - 12위</a> </li><li> <a href="/digital-electronics-adders/">디지털 전자제품 - 가산기</a> </li><li> <a href="/http-headers/">Http 헤더</a> </li><li> <a href="/javascript-functions/">자바스크립트 함수</a> </li><li> <a href="/directi/">감독</a> </li>
                                
                            </ul>
                        </div><!--//content-->  
                    </div><!--//section-inner-->                 
                </aside><!--//aside-->
                
                 <aside class="testimonials aside section">
                   <div class="section-inner">
                        <h2 class="heading">라텍스 분획</h2>
                        <div class="content">
                            <div class="item">
                                 <blockquote class="quote">                                  
                                    <span> <i class="fa fa-quote-left"></i> LaTeX 튜토리얼을 사용한 라텍스 분수, LaTeX 설치, LaTeX 다운로드, LaTeX 편집기, LaTeX 사용 방법, LaTeX 기호, LaTeX 목록, LaTeX 파일 형식, LaTeX 글꼴, LaTeX 테이블, LaTeX Texmaker 등.</span>
                                </blockquote>                
                                                                                             
                            </div><!--//item-->
                            
                            <p> <a class="more-link" href="/latex-fractions"> <i class="fa fa-external-link"></i> 자세히보기</a> </p> 
                            
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
                
               
                            
                 <aside class="languages aside section">
                    <div class="section-inner">
                        <h2 class="heading">재미있는 기사</h2>
                        <div class="content">
                            <ul class="list-unstyled">
                                <li class="item">
                                    <span class="title"> <strong> <a href="/how-sort-list-c-list">C#에서 목록을 정렬하는 방법 | List.Sort() 메서드 세트 -1</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/introduction-dbms-set-1">DBMS(데이터베이스 관리 시스템) 도입 – Set 1</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/linux-telnet-command">리눅스 텔넷 명령</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/software-engineering/">소프트웨어 공학</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/paging-operating-system">운영 체제의 페이징</a> </strong> </span>
                                    
                                </li><!--//item-->
                               
                            </ul>
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
                
              
                 <aside class="list music aside section">
                    <div class="section-inner">
                        <h2 class="heading">인기 게시물</h2>
                        <div class="content">
                            <ul class="list"> <li><a href="/exception-handling-java">예외 처리 자바</a>
</li><li><a href="/how-convert-byte-array-string-java">바이트 배열을 문자열로 변환</a>
</li><li><a href="/java-8-features">자바 8 기능</a>
</li><li><a href="/java-xor">자바의 xor</a>
</li><li><a href="/how-take-string-input-java">자바에서 문자열 입력하기</a>
</li><li><a href="/how-create-array-objects-java">자바 객체 배열</a>
</li><li><a href="/what-is-trie-data-structure">트라이 데이터 구조</a>
</li> 
                                
                                
                            </ul>
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
              
            </div><!--//secondary-->    
        </div><!--//row-->
    </div><!--//masonry-->
    
	    <footer class="footer">
        <div class="container text-center">
                <span>
Copyright ©2025 판권 소유 |  <a href="//ro.techcodeview.com/">techcodeview.com</a> | <a href="/disclaimer" rel="nofollow noopener noreferrer" target="_blank">부인 성명</a>  |  <a href="/about-us" rel="nofollow noopener noreferrer" target="_blank">회사 소개</a>  |  <a href="/privacy-policy" rel="nofollow noopener noreferrer" target="_blank">개인 정보 보호 정책</a>  </span>
        </div>
    </footer>
 
         
    <script type="text/javascript" src="https://techcodeview.com/template/assets/plugins/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://techcodeview.com/template/assets/plugins/bootstrap/js/bootstrap.min.js"></script>    
    

    <script type="text/javascript" src="https://techcodeview.com/template/assets/js/main.js"></script>     
	
	<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
	
</body>
</html>