[英]jQuery plugin override parameters
In this plugin want to change background color with jQuery. 在此插件中,要使用jQuery更改背景颜色。 default background color is red, but when i am trying to override parameters then don't work that, here i am trying to set background color green. 默认的背景颜色是红色,但是当我尝试覆盖参数然后不起作用时,在这里我试图将背景颜色设置为绿色。 but not working, that is still red 但不起作用,仍然是红色
here is code on plugin file 这是插件文件上的代码
(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);
here is code on html file 这是HTML文件上的代码
<p class="ele">dfdfg</p>
$(".ele").myBgcolor({
bgColor: "green"
});
I am not sure what you are trying to achieve by the following two lines as this
already refers to the jQuery object. 我不确定您要通过以下两行尝试实现什么,因为this
已经指向jQuery对象。
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
First mistake here is the extra argument el
that refers to the options not to the element, so you have to remove it: 这里的第一个错误是多余的参数el
,它指向选项而不是元素,因此必须将其删除:
$.fn.myBgcolor = function(/* el, */ options)
And then your constructor should become like this: 然后您的构造函数应如下所示:
$.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();
};
See the sample here http://jsfiddle.net/7Rrs3/1/ 在此处查看示例http://jsfiddle.net/7Rrs3/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.