jQuery 문에서 javascript 함수를 호출하는 데 문제가 있습니다. 자바 스크립트 함수 안에 jQuery가 작동하지 않는 것 같습니다. 구문 오류가 있습니까? 나는 filterImage 기능을 내부에 더 많은 기능을 필요로하기 때문에jquery를 사용하여 javascript 함수를 호출하지 못했습니다.
jQuery(function ($) {
// init the state from the input
$(".image-checkbox").each(function() {
if ($(this).find('input[type="checkbox"]').first().attr("checked")) {
$(this).addClass('image-checkbox-checked');
}
else {
$(this).removeClass('image-checkbox-checked');
}
});
// sync the state to the input
$(".image-checkbox").on("click", function (e) {
if ($(this).hasClass('image-checkbox-checked')) {
$(this).removeClass('image-checkbox-checked');
$(this).find('input[type="checkbox"]').first().removeAttr("checked");
}
else {
$(this).addClass('image-checkbox-checked');
$(this).find('input[type="checkbox"]').first().attr("checked", "checked");
}
e.preventDefault();
});
});
구조가 변경되고, 브래킷 내부의 문이 제대로 실행하지 못했습니다 :
코드
이로 일했다.$(".image-checkbox").on("click", function(e) {
filterImage();
});
function filterImage() {
$(document).ready(function() {
if ($(this).hasClass('image-checkbox-checked')) {
$(this).removeClass('image-checkbox-checked');
$(this).find('input[type="checkbox"]').first().removeAttr("checked");
}
else {
$(this).addClass('image-checkbox-checked');
$(this).find('input[type="checkbox"]').first().attr("checked", "checked");
}
});
}
이벤트 처리기 주변의'$ (document) .ready (function() {')을 감싸고 함수에서 제거하십시오. – empiric
콘솔에 오류가 있습니까? 함수 또는 클릭 이벤트 자체가 실행됩니까? – empiric
@empiric, Thanks 귀하의 신속한 회신을 위해! 내가 본 것에서는 if-else 브래킷 내부의 문장이 제대로 실행되지 않아서 항상 else 부분으로 들어간다. – Victor