먼저 선택한 요소를 초기화하려는 jq-plugin을 작성했습니다 (모두 모두). 나중에 코드에서 메서드로 생성 된 문자열을 가져 오려고합니다. 그러나 그것이 반환하는 것은 객체이지만 내 문자열은 아닙니다.jquery 플러그인 메소드 반환 값 객체가 아닙니다.
나는 인터넷에서 많은 연구를했지만, 한편으로는 '체인을 연결할 수있는'방법을, 다른 한편에서는 '어떤 값을 반환하는'방법을 모르겠다.
어떻게 생각하십니까?
(function($){
var methods = {
init: function(val){
// Actions to initialize ALL selected Elements
}
,
returner: function(val){
alert('working with every selected element: '+$(this).width());
// return anything
return 'STRING PRODUCED BY THIS FUNCTION!';
}
}
$.fn.myplug = function(method){
var args = arguments;
var init = 'init-myplug';
return this.each(function(){
var is_init = $(this).data(init);
if (is_init && methods[method]){
return methods[method].apply($(this), Array.prototype.slice.call(args, 1));
} else if (!is_init && (typeof method === 'object' || !method)){
$(this).data(init, true);
return methods.init.apply($(this), Array.prototype.slice.call(args, 0));
}
});
};
})(jQuery);
$('.selects_5_elements').myplug(); // init
$('.selects_5_elements').myplug('returner'); // get the string
, 이것은 아주 오래된 스레드입니다! – Strawberry
나는 여기에서 새로운 ... 나는 질문 날짜를 보지 못했다. 죄송합니다 –