2017-04-06 1 views
0

에 대한 클릭을 취소 : 제품 이미지에기능 해제() 나는 코드 조각이있는 모든 요소

jQuery(function($){ 
    $(document).on("click", ".product-image-mobile .product-image", function (e) { 
     $(this).attr('data-num', 1); 
     e.preventDefault(); 
     if ($(this).attr("data-num") == 1){ 
      $(document).off("click", ".product-image-mobile .product-image"); 
     } 
    }); 
}); 

먼저 클릭 다른 이미지를 표시, 다시 클릭하면 제품 페이지로 이동합니다. ajax를 통해로드하는 제품. 첫 번째 제품 이미지는이 코드가 작동하고 있지 않은 제품도 있습니다. 나는 그것이 다른 모든 요소들에 대해 "예방"을 클릭하는 것을 취소하는 $ (document) .off와 관련 있다고 생각한다.

어떻게 수행 할 수 있는지 알려주십시오. 감사.

+0

뭐라구? 정교하게하십시오 –

+0

정확하게 당신은 문서에 대한 클릭 이벤트를 바인딩 해제하여 달성하려고 무엇입니까? –

답변

0

:not()Attribute value selector의 조합을 사용하여 이벤트를 바인딩 할 수 있습니다.

$(document).on("click", ".product-image-mobile .product-image:not([data-num='1'])", function (e) { 
    $(this).attr('data-num', 1); 
    e.preventDefault();   
}); 

나는 잘 모르겠지만, 난 당신이 또한 당신은 당신의 선택을 확장 할 수

if ($(this).attr("data-num") == 1){ 
     $(event.target).off("click"); 
    } 
+0

많은 것들! 이 솔루션을 잊어 버렸습니다! –

0

를 사용 할 수 있다고 생각이 같은 속성 data-num="1"와 더 이상 선택 요소 :

jQuery(function($){ 
    $(document).on("click", ".product-image-mobile .product-image[data-num!='1']", function (e) { 
     $(this).attr('data-num', 1); 
     e.preventDefault(); 
    }); 
}); 

도 참조하십시오. attribute-not-equal-selector

+0

많은 것보다! 이 솔루션을 잊어 버렸습니다! –