2012-11-24 3 views
1

jQuery 플러그인을 사용하여 간단한 jQuery 플러그인을 만들려고합니다. (예, 이미 jQuery와 공식 플러그인이 있습니다. 개인적인). 나는 기본적으로 코딩되어있다. 내가 할 수있는 유일한 방법은 어떤 버튼을 클릭했는지 탐지하고, 그 호출을 기반으로 플러그인을 통해 제공되는 해당 함수를 호출하는 것이다. 이것을하는 가장 좋은 방법은 무엇입니까?클릭 이벤트에 따라 jQuery Plugin 이벤트가 발생했습니다.

(function($){ 
$.fn.extend({ 

    confirmDialog: function(options) { 

     var defaults = { 
      titleText: "Confirmation", 
      bodyText: "Are you sure you want to delete this?", 
      confirmButtonText: "Delete", 
      confirmAction: function(){}, 
      cancelAction: function(){}, 
     }; 

     var options = $.extend(defaults, options); 

     return this.each(function() { 

    $('<div class="confirmation-box-wrapper"><div class="confirmation-box"><div class="confirmation-title">'+options.titleText+'</div><div class="confirmation-message">'+options.bodyText+'</div><div class="options"><a class="buttons cancel">Cancel</a><a class="buttons confirmation">'+options.confirmButtonText+'</a></div></div></div>').appendTo('body'); 



     }); 
    } 
}); 
})(jQuery); 

그래서 나는 사용이처럼되고 무엇을 기대 이것이다 : 당신은 그 확장의 버튼을 바인드해야

$(".elementToDelete").confirmDialog({ 
     confirmAction: function(){ alert("Deleted"); }, 
     cancelAction: function(){ alert("Cancelled"); } 

    }) 

답변

2

그래서 여기에 지금까지 내 코드입니다. http://jsfiddle.net/4mjBb/

var wrapper=$('<div class="confirmation-box-wrapper"... ...')appendTo('body'); 

wrapper.find('.confirmation').on('click', function(){ 
    if (typeof options.confirmAction=='function') options.confirmAction(); 

}); 
wrapper.find('.cancel').on('click', function(){ 
    if (typeof options.cancelAction=='function') options.cancelAction(); 
}); 
+0

완벽! 무리 감사! –

관련 문제