나는 init, close, open 함수가있는 간단한 플러그인을 가지고있다. 이 플러그인을 호출하는 html 템플릿 배열이 있습니다. 특정 템플릿에 대해서만이 플러그인과 약간 다른 동작을하고 싶습니다. 열린 함수에 다른 클래스를 추가하고 닫으면 같은 클래스를 제거 할 수 있습니다. 그것을하는 우아한 방법은 무엇입니까? html의 ID를 찾아서 같은 플러그인 내에서 열고 닫는 함수 내에서 else를해야합니까 아니면 더 좋은 방법이 있을까요?약간 다른 동작으로 플러그인을 호출하는 방법은 무엇입니까?
;(function ($, window, document, undefined) {
function Plugin(element, options) {
Window = this;
this.element = element;
this._name = pluginName;
this.init(element);
}
Plugin.prototype = {
init: function(element) {
},
close:function(e){
//removes a class and hides the element
},
open:function(element){
//adds a class and shows the element
}
}
//Extend Global jQuery (where we actually add the plugin!)
$.fn[pluginName] = function (options) {
plugin = $.data(window, 'plugin_' + pluginName);
if (!(plugin instanceof Plugin)) {
$.data(window, 'plugin_' + pluginName,
plugin = new Plugin(this, options));
}
return $Extend(this).each(function() {
$.data(this, 'plugin_' + pluginName, plugin);
});
};
}(jQuery, window, document));
또는 대부분의 jQuery 플러그인에서 사용되는'extend()'관용구를 사용하여 사용자 옵션을 기본 옵션 오브젝트로 병합하십시오. – Barmar
당신이해야 할 다른 일은 각 요소에 사용 된 옵션을 기억하는 것입니다. 이것은'data()'를 사용하여 수행 할 수 있습니다. – Barmar
@Barmar 두 가지 좋은 포인터. – adamb