jQuery

jQuery - .click()

행복하게사는게꿈 2020. 6. 23. 15:31

.click()

 

 - 선택한 요소를 클릭했을 때 특정 작업을 수행할 수 있게 하는 속성

 

문법

.click( handler )

예를 들어 button 요소를 클릭했을 대 함수를 실행시키고 싶으면 다음과 같다.

$( 'button' ).click( function() {

  // function
  
} );

예제

 

 - 문단을 클릭하면 테두리가 생긴다. 다시 클릭하면 테두리가 사라진다.

<!doctype html>

<html lang="ko">

  <head>
  
    <meta charset="utf-8">
    
    <title>jQuery</title>
    
    <style>
    
      p {
      
        cursor: pointer;
      }
      
      .jbBox {
      
        border: 1px solid #bcbcbc;
        
        padding: 20px;
        
      }
    </style>
    
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    
    <script>
    
      $( document ).ready( function() {
      
        $( 'p' ).click( function() {
        
          $( this ).toggleClass( 'jbBox' );
          
        } );
        
      } );
      
    </script>
    
  </head>
  
  <body>
  
    <p>Lorem ipsum dolor.</p>
    
  </body>
</html>