Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 납입
- 인출수수료
- 급성심근경색증
- 보험
- PythonProgramming
- 가입
- 리스트
- 중도인출
- 특약
- 코딩
- Java
- 자바스크립트
- 수수료
- 웹개발
- 교보생명
- python
- 사망
- 파이썬
- 변환
- javascript
- 추가납입
- 심장질환
- 뇌출혈
- jQuery
- 프론트엔드
- 프로그래밍
- 교보
- 보험료
- 문자열
- Vue.js
Archives
- Today
- Total
SeouliteLab
jQuery .off() 메서드: 이벤트 해제를 위한 메서드 본문
예제 1: 클릭 이벤트 제거하기
<!DOCTYPE html>
<html>
<head>
<title>jQuery .off() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("클릭 이벤트가 발생했습니다.");
});
$("#removeButton").click(function(){
$("button").off("click");
alert("클릭 이벤트가 제거되었습니다.");
});
});
</script>
</head>
<body>
<button>클릭하세요</button>
<button id="removeButton">이벤트 제거</button>
</body>
</html>
설명: 이 예제에서는 버튼을 클릭할 때 경고 메시지가 표시되며, "이벤트 제거" 버튼을 클릭하면 클릭 이벤트가 제거됩니다.
예제 2: 특정 이벤트 핸들러만 제거하기
<!DOCTYPE html>
<html>
<head>
<title>jQuery .off() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
function clickHandler1() {
alert("첫 번째 클릭 이벤트가 발생했습니다.");
}
function clickHandler2() {
alert("두 번째 클릭 이벤트가 발생했습니다.");
}
$("button").click(clickHandler1);
$("button").click(clickHandler2);
$("#removeButton").click(function(){
$("button").off("click", clickHandler1);
alert("첫 번째 클릭 이벤트 핸들러가 제거되었습니다.");
});
});
</script>
</head>
<body>
<button>클릭하세요</button>
<button id="removeButton">첫 번째 이벤트 핸들러 제거</button>
</body>
</html>
설명: 이 예제에서는 두 개의 클릭 이벤트 핸들러를 연결한 후, 첫 번째 클릭 이벤트 핸들러만 제거하는 방법을 보여줍니다.
예제 3: 모든 이벤트 핸들러 제거하기
<!DOCTYPE html>
<html>
<head>
<title>jQuery .off() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("클릭 이벤트가 발생했습니다.");
});
$("#removeAllButton").click(function(){
$("button").off();
alert("모든 이벤트 핸들러가 제거되었습니다.");
});
});
</script>
</head>
<body>
<button>클릭하세요</button>
<button id="removeAllButton">모든 이벤트 제거</button>
</body>
</html>
설명: 이 예제에서는 모든 버튼에 연결된 이벤트 핸들러를 제거하는 방법을 보여줍니다.
예제 4: 이벤트 네임스페이스를 이용한 제거
<!DOCTYPE html>
<html>
<head>
<title>jQuery .off() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").on("click.myNamespace", function(){
alert("클릭 이벤트가 발생했습니다.");
});
$("#removeButton").click(function(){
$("button").off(".myNamespace");
alert("네임스페이스가 myNamespace인 모든 이벤트 핸들러가 제거되었습니다.");
});
});
</script>
</head>
<body>
<button>클릭하세요</button>
<button id="removeButton">이벤트 제거</button>
</body>
</html>
설명: 이 예제에서는 이벤트의 네임스페이스를 이용하여 해당 네임스페이스에 속한 이벤트 핸들러만 제거하는 방법을 보여줍니다.
예제 5: 한 번만 실행되는 이벤트 핸들러 제거하기
<!DOCTYPE html>
<html>
<head>
<title>jQuery .off() 메서드 예제</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
function clickHandler() {
alert("이벤트 핸들러가 한 번 실행되었습니다.");
$(this).off("click", clickHandler);
}
$("button").click(clickHandler);
});
</script>
</head>
<body>
<button>클릭하세요</button>
</body>
</html>
설명: 이 예제에서는 이벤트 핸들러가 한 번 실행되고 나서 자동으로 제거되는 방법을 보여줍니다.
'프로그래밍' 카테고리의 다른 글
jQuery .one() 메서드: 이벤트 핸들러 단 한 번만 실행하기 (0) | 2024.04.04 |
---|---|
jQuery .on() 메서드: 이벤트 핸들러 등록을 위한 메서드 (0) | 2024.04.04 |
jQuery .mouseup() 메서드: 마우스 버튼을 놓을 때 발생하는 이벤트 (0) | 2024.04.04 |
jQuery .mouseover() 메서드: 요소에 마우스를 올렸을 때 발생하는 이벤트 (0) | 2024.04.04 |
jQuery .mouseout() 메서드 이해하기: 요소를 벗어날 때 발생하는 이벤트 (0) | 2024.04.04 |