2014-04-28 3 views
0

이 플러그인에서 jQuery로 배경색을 변경하고 싶습니다. 기본 배경색은 빨간색이지만, 매개 변수를 무시하려고 할 때 작동하지 않습니다. 여기 배경색을 녹색으로 설정하려고합니다. 하지만 여전히 빨간색, 작동하지 여기jQuery 플러그인 대체 ​​매개 변수

플러그인 파일 여기

(function($){ 
    if(!$.fn){ 
     $.fn = new Object(); 
    }; 

    $.fn.myBgcolor = function(el, options){ 
     // To avoid scope issues, use 'base' instead of 'this' 
     // to reference this class from internal events and functions. 
     var base = this; 

     // Access to jQuery and DOM versions of element 
     base.$el = $(el); 
     base.el = el; 

     // Add a reverse reference to the DOM object 
     base.$el.data("fn.myBgcolor", base); 

     base.init = function(){ 
      base.options = $.extend({},$.fn.myBgcolor.defaultOptions, options); 

      // Put your initialization code here 
     }; 

     // Sample Function, Uncomment to use 
     base.BGcolor = function(paramaters){ 
     base.css("background-color", base.options.bgColor); 
     }; 

     // Run initializer 
     base.init(); 
     base.BGcolor(); 
    }; 

    $.fn.myBgcolor.defaultOptions = { 
     bgColor: "red" 
    }; 

    $.fn.fn_myBgcolor = function(options){ 
     return this.each(function(){ 
      (new $.fn.myBgcolor(this, options)); 
     }); 
    }; 

    // This function breaks the chain, but returns 
    // the fn.myBgcolor if it has been attached to the object. 
    $.fn.getfn_myBgcolor = function(){ 
     this.data("fn.myBgcolor"); 
    }; 

})(jQuery); 

에 대한 코드는 HTML 파일에 대한 코드가 있습니다 난 당신이에 의해 달성하려고하는 것을 확실하지 않다

<p class="ele">dfdfg</p> 

$(".ele").myBgcolor({ 
bgColor: "green" 
}); 

답변

1

this은 이미 jQuery 객체를 참조하기 때문에 두 줄을 따라 가야합니다.

$.fn.myBgcolor = function(/* el, */ options) 

을 그리고 생성자는 다음과 같이 될해야합니다 : 당신이 그것을 제거 할 필요가 있으므로 여기

// Access to jQuery and DOM versions of element 
base.$el = $(el); 
base.el = el; 

첫 번째 실수는, 요소에없는 옵션을 의미 여분의 인수 el입니다

$.fn.myBgcolor = function(options){ 
    // To avoid scope issues, use 'base' instead of 'this' 
    // to reference this class from internal events and functions. 
    var base = this; 

    // Add a reverse reference to the DOM object 
    base.data("fn.myBgcolor", base); 

    base.init = function(){ 
     base.options = $.extend({},$.fn.myBgcolor.defaultOptions, options); 
     // Put your initialization code here 
    }; 

    // Sample Function, Uncomment to use 
    base.BGcolor = function(paramaters){ 
     base.css("background-color", base.options.bgColor); 
    }; 

    // Run initializer 
    base.init(); 
    base.BGcolor(); 
}; 

여기에서 샘플보기 http://jsfiddle.net/7Rrs3/1/

관련 문제