2011-02-25 5 views
0

사용자가 "a.open-popup"을 클릭하면 변경할 HTML (1 기능)과 팝업 (간단한 기능)이 필요합니다. 어떻게해야합니까? 나는 2 개의 함수 중 1 개만 트리거 할 수 있습니다.1 개의 선택기와 함께 2 개의 이벤트를 트리거하는 방법은 무엇입니까?

HTML :

<div> 
     <p class="plan-msg">Plan message</p> 
     <div class="completed-button"> 
       <a href="#popup01" class="open-popup">Did it</a> 
     </div> 
</div> 

의 jQuery 함수 1 (HTML 변경)

jQuery('a.open-popup').click(function(event){ 
var $parent = jQuery(this).parent(); 

    event.preventDefault(); 
    $parent.siblings('.plan-msg').remove(); 
    $parent.removeClass('completed-button') 
       .addClass('add-inv-button ') 
       .html('Add to Plan'); 

}); 

의 jQuery 함수 2 (오픈 simplelightbox)

$(function(){ 
    jQuery("a.open-popup").click(function(event) { 
      event.preventDefault(); 
      jQuery(this).simpleLightbox({ closeLink:'a.close' }); 
    }); 
}) 

감사합니다.

답변

0
jQuery("a.open-popup").simpleLightbox({ closeLink:'a.close' }); 

당신은 .click()을 놓치고있어. 그것은해야한다 :

jQuery("a.open-popup").click(function(evt) { 
    evt.preventDefault(); 
    jQuery(this).simpleLightbox({ closeLink:'a.close' }); 
}); 

것이다 이미 정확 다른 핸들러와 함께 작동합니다.


확인, 편집, 다음이 작동하면 코드의 simpleLightbox 전에이 블록의 코드를 포함시켰다 경우 가 실행됩니다

$(function() { 
    jQuery("a.open-popup").simpleLightbox({ 
     closeLink: 'a.close' 
    }); 

    jQuery('a.open-popup').click(function(event) { 
     var $parent = jQuery(this).parent(); 

     event.preventDefault(); 
     $parent.siblings('.plan-msg').remove(); 
     $parent.removeClass('completed-button') 
      .addClass('add-inv-button') 
      .html('Add to Plan'); 

    }); 
}); 

난을 게시 jsFiddle http://jsfiddle.net/fZPjg/에서이 작업 예제.

참고 : 원래 게시물과 일치하도록 $(function() {...}) 표기법을 사용했지만, jQuery는 $(document).ready(function() { ... })을 대신 사용하는 것이 좋습니다. 현재 두 양식은 동일하게 기능합니다.

+0

이것은 작동하지 않는 것 같습니다. a.open-popup을 클릭하면 브라우저가 div # popup01을 열지 않습니다. 대신 www.mydomain.com/#popup01로 이동하면 화면이 비어 있습니다. – JMan

+0

링크를 클릭하는 기본 동작이므로 페이지가 탐색 중입니다. 위와 같이 탐색을 방해하는 편집을했습니다. 문제가 해결되지 않으면 사용중인'simpleLightbox' 플러그인의 문서에 대한 링크를 게시하십시오. 그러면 더 도움이 될지 알 수 있습니다. – Lee

+0

두 jQuery 함수 모두 event.preventDefault를 포함하도록 위 코드를 업데이트했습니다. 그러나 지금은 원래의 문제로 돌아 왔습니다. 2 개의 이벤트 중 하나만 트리거되지만 둘다는 트리거하지 않습니다. 다음은 simpleLightbox 코드에 대한 링크입니다. http://bit.ly/fp7FjY – JMan

0

당신이 첫 번째 이벤트의 핸들러 내부에있는 경우 사용자 정의 트리거를 해고 .trigger을 사용할 수 있습니다, 여기에 DEMO

관련 문제