클릭에 대해 이벤트 리스너를 사용할 수 있습니다.
예시
다음은 코드입니다 -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="divDemo">
First Division
</div>
<div class="divDemo">
Second Division
</div>
</body>
<script>
document.addEventListener('click', callEventFuncion)
function callEventFuncion(event) {
var div = document.querySelectorAll('.divDemo');
var titleResult = document.querySelectorAll('.my-title');
var result = Array.apply(0, div).find((v) => v.contains(event.target));
if (result) {
console.log(" Incrementing Division Selection");
}
else {
console.log("Incrementing Element Only OutSide of DIV");
}
}
</script>
</html> 위의 프로그램을 실행하기 위해서는 파일명을 “anyName.html(index.html)”로 저장합니다. 파일을 마우스 오른쪽 버튼으로 클릭하고 Visual Studio Code 편집기에서 "라이브 서버로 열기" 옵션을 선택합니다.
출력
이것은 콘솔에 다음과 같은 출력을 생성합니다 -

분할을 클릭할 때마다 분할 부분이 증가합니다. 그렇지 않으면 분할 외부에서 증가합니다.
사례 1
사용자가 div 섹션을 클릭할 때. 여기서는 div 섹션을 두 번 클릭했습니다. 스냅샷은 다음과 같습니다 -

콘솔의 출력 -

사례 2
이제 div 섹션 외부를 일곱 번 클릭합니다. 스냅샷은 다음과 같습니다 -
이것은 콘솔에서 다음과 같은 출력을 생성합니다 -
