WEB(웹)/jQuery
기본 이벤트와 이벤트 전달
SharingWorld
2018. 7. 2. 10:54
기본 이벤트와 이벤트 전달
메서드 이름 | 설명 |
---|---|
preventDefault() | 기본 이벤트를 제거합니다. |
stopPropagation() | 이벤트 전달을 제거합니다. |
$(document).ready(function() {
$('a').click(function event) {
$(this).css('background-color', 'blue');
event.stopPropagation(); // h1 클릭까지 되는걸 없애기.
event.preventDefault(); // a의 본연의 링크 이벤트 기능을 없애기
});
$('h1').click(function() {
$(this).css('background-color', 'red');
});
});
위의 코드를 간단하게 return false; 로 구현할 수 있다.
$('a').click(function(event) {
$(this).css('background-color', 'blue');
return false;
});