2014-02-27 4 views
-1

로그인 폼에 fancybox를 사용하고 있습니다. 유효성 검사가 성공적으로 완료되면 afterClose 콜백 내에서 일부 코드를 실행하고 fancybox를 수동으로 닫습니다.Fancybox afterClose 함수에서 클릭 된 앵커/요소의 ID를 가져옵니다.

닫기 버튼 [X]을 사용하는 닫기 fancybox의 경우 내 코드가 afterClose 방법으로 실행됩니다. 닫기 [X] 버튼을 사용하여 fancybox를 닫은 경우 어떻게 감지합니까? 또는 현재 click을 추적하려면 어떻게해야합니까? 그래서 내 코드를 실행할 것인지 말 것인지를 결정할 수 있습니다.

몇 가지 트릭을 시도했지만 여전히 작동하지 않습니다.

+0

을' 클릭 한 요소의 'id'? 여기를 읽으면서, 당신이 필요로하는 것을위한 방법이 있습니다. http://fancybox.net/api – rockStar

답변

2

문제가 fancybox (버전 2.x) 닫기 버튼이 ID을하지 않는다는 것입니다, 그것은 클래스 만, 그러나 당신이 [X] 버튼에 click 이벤트를 결합하고 당신의 상태를 설정할 수 있습니다 경우 [X] 닫기 버튼을 눌렀는지 여부.

currentTargetclick 이벤트와 같은 변수에 잡기보십시오 : 당신이 원하는 작업,

var eTarget; 
jQuery(document).ready(function ($) { 
    $(".fancybox").fancybox({ 
     afterShow: function() { 
      // bind a click event to fancybox close button 
      // set the value of the currentTarget to the eTarget variable 
      eTarget=""; // reset variable 
      $(".fancybox-close").on("click", function (event) { 
       eTarget = event.currentTarget; 
      }); 
     }, 
     afterClose: function() { 
      // validate the eTarget variable 
      if ($(eTarget).hasClass("fancybox-close")) { 
       // yes, we are closing fancybox with the [X] button 
       // so close it, do nothing and return 
       console.log("fancybox was closed using the X button"); 
       return; 
      } 
      // only if fancybox wasn't close using the X button 
      // executed my validation and manual close 
      console.log("perform form submission and validation here") 
     } 
    }); 
}); // ready 

JSFIDDLE

0

이 시도 참조

console.log(this.element.context.id);         
관련 문제