我正在创建一个简单的插件,将创建一个子元素为每个选定的元素。我的问题是插件没有返回对子元素的引用。(或者我不知道如何访问它)下面是这个插件(为了简单起见,您可以假定省略了缺少的部分):
(function($) {
$.fn.ui_child = function(options) {
this.each(function() {
return ui_child($(this), options);
})
}
ui_child = function (target, options) {
return new ui_fn_child(target, options);
}
ui_fn_child = function (target, options) {
this.target = target;
this.settings= $.extend({}, $.fn.ui_child.defaults, options);
this.child_create()
return this.child
}
})(jQuery);如果我执行以下操作:
var child = $('selector').ui_child();子变量没有引用任何内容。理想情况下,我希望能够使用变量访问它。谢谢你的帮助
发布于 2011-05-25 13:57:04
如果您以这样的方式更改ui_child方法:
$.fn.ui_child = function(options) {
var children = [];
this.each(function() {
children.push(ui_child($(this), options));
})
return children;
}你将在这里得到一个孩子的数组:
var child = $('selector').ui_child();发布于 2011-05-25 14:55:36
使用关键字new使函数返回this对象。
也许你应该用不同的方式来思考:
ui_child = function (target, options) {
return ui_fn_child(target, options); // no new keyword
};
ui_fn_child = function (target, options) {
var o = {}; // explicit object creation
o.target = target;
o.settings= $.extend({}, $.fn.ui_child.defaults, options);
o.child_create();
return o.child;
};https://stackoverflow.com/questions/6120031
复制相似问题