jQuery插件覆盖参数
jQuery plugin override parameters
在这个插件中,想要用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"
});
我不确定您试图通过以下两行实现什么,因为this
已经引用了jQuery对象。
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
这里的第一个错误是额外的参数el
,它指的是选项而不是元素,所以你必须删除它:
$.fn.myBgcolor = function(/* el, */ options)
然后你的构造函数应该变成这样:
$.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/
相关文章:
- 在循环中分配json值时,值被覆盖
- 函数参数中的数据与指定变量之间的任何性能差异
- 为什么ES6 Fiddle没有't覆盖命名参数
- 在绑定多个动态 JQGard 时覆盖参数
- 覆盖 JQGrid 参数在服务器端找到
- 如何在不编辑的情况下覆盖插件的几个参数
- 如何在不覆盖父范围的情况下将参数传递给指令
- AngularJS-自动向URL添加哈希标签-覆盖搜索参数
- 参数是否可以被覆盖
- 如何在javascript中用可变数量的参数覆盖函数
- 当名称被参数覆盖时访问自身的函数
- 覆盖og:description ví一个参数
- 为什么我的jquery new Date()函数会覆盖传入的参数?
- 主干模型url选项覆盖url参数
- 在JQuery/Backbone中,我如何覆盖每个AJAX请求来添加一个额外的参数?
- 如何覆盖整个项目的默认参数
- jQuery Ajax:成功响应参数覆盖
- jQuery插件覆盖参数
- 如何将参数传递到模块模式以覆盖JavaScript中的默认值[private properties]
- 如何覆盖参数的默认值