jQuery / Method / .click() - 선택한 요소를 클릭했을 때 특정 작업을 하는 메서드
.click()
.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>