클릭 이벤트 핸들러 추가
이 단계에서는 페이지 상단의 과일 아이템에 클릭 이벤트 핸들러를 추가합니다.
index.html 파일에서 <script> 태그 내의 TODO 섹션을 찾습니다.
<script> 태그는 ids 변수의 빈 배열을 설정합니다.
let ids = [];
$("#card li").on("click", function (e) {
// TODO: Please implement the function here
});
- TODO 섹션 안에 다음 코드를 추가합니다:
$("#card li").on("click", function (e) {
// TODO: Please implement the function here
if ($("#box li").length >= 7) {
return;
}
let clone = $(this).clone();
$("#box").append(clone);
let currentId = $(this).data("id");
ids.push($(this).data("id"));
let currentIdLen = ids.filter((id) => id == currentId)?.length;
if (currentIdLen == 3) {
ids = ids.filter((id) => id !== currentId);
let three = $(`#box li[data-id=${currentId}]`);
for (let index = 0; index < three.length; index++) {
const element = three[index];
$(element).addClass("active");
setTimeout(() => {
element.remove();
}, 200);
}
}
$(this).css({
top: "600px",
left: "200px",
opacity: 0,
transition:
"left .2s linear, top .2s cubic-bezier(.08,-0.35,.99,.33),opacity .2s linear"
});
});
이 코드는 페이지 상단의 과일 아이템에 클릭 이벤트 핸들러를 추가합니다. 과일 아이템을 클릭하면 복제되어 페이지 하단의 상자에 추가됩니다. 상자에 동일한 과일이 세 개 있으면 제거됩니다. 하단 사각형 (id=box) 요소에 7 개의 과일이 있고 제거할 수 없는 경우, 클릭된 과일 요소 노드는 추가된 사각형 요소에 포함되지 않습니다.