特定对象的 jQuery 插件命名空间
我正在尝试创建一个 jQuery 插件,它将向上下文对象添加新的命名空间函数,同时保持完整的链能力。我不确定这是否可能,但这是我迄今为止所拥有的一个示例:
(function ($) {
var loadScreen = $('<div />').text('sup lol');
$.fn.someplugin = function (args) {
var args = args || {},
$this = this;
$this.append(loadScreen);
return {
'caption' : function (text) {
loadScreen.text(text);
return $this;
}
};
}
})(jQuery);
如果我这样做 $(document.body).someplugin().caption('hey how\'s it go ?').css('background-color', '#000');
但是我还需要能够执行 $(document.body).someplugin().css('background-color ', '#000').caption('嘿,进展如何?');
由于 .someplugin()
返回它自己的对象,而不是 jQuery 对象,因此它不会按预期工作。我还需要能够稍后通过 $(document.body)
访问 .caption()
。例如,如果没有为初始 $(document.body).someplugin()
设置变量。这意味着如何通过 $.fn.caption = function () ...
为 document.body 设置
.caption()
对象。这是我不太确定是否可能的部分。如果没有,那么我想我将不得不满足于要求设置一个变量,以维持插件函数的链能力。
这是我期望的示例代码:
$(document.body).someplugin().css('background-color', '#000');
$('.non-initialized').caption(); // Error, jQuery doesn't know what caption is
$(document.body).caption('done loading...');
如果这是不可能的,或者效率很低,那么我愿意解决这个问题:
var $body = $(document.body).someplugin().css('background-color', '#000');
$('.non-initialized').caption(); // Error, jQuery doesn't know what caption is
$body.caption('done loading...');
I'm am trying to create a jQuery plugin that will add new namespace functions to the context object(s), while maintaining full chain-ability. I'm not sure if it's possible, but here's an example of what I have so far:
(function ($) {
var loadScreen = $('<div />').text('sup lol');
$.fn.someplugin = function (args) {
var args = args || {},
$this = this;
$this.append(loadScreen);
return {
'caption' : function (text) {
loadScreen.text(text);
return $this;
}
};
}
})(jQuery);
This works fine if I do $(document.body).someplugin().caption('hey how\'s it going?').css('background-color', '#000');
However I also need the ability to do $(document.body).someplugin().css('background-color', '#000').caption('hey how\'s it going?');
Since .someplugin()
returns it's own object, rather than a jQuery object, it does not work as expected. I also need to be able to later on access .caption()
by $(document.body)
. So for example if a variable is not set for the initial $(document.body).someplugin()
. This means that somehow how .caption()
is going to be set through $.fn.caption = function () ...
just for the document.body
object. This is the part which I'm not quite sure is possible. If not, then I guess I'll have to settle for requiring that a variable to be set, to maintain plugin functions chain-ability.
Here's an example code of what I expect:
$(document.body).someplugin().css('background-color', '#000');
$('.non-initialized').caption(); // Error, jQuery doesn't know what caption is
$(document.body).caption('done loading...');
Here's what I'm willing to settle for if that is not possible, or just very inefficient:
var $body = $(document.body).someplugin().css('background-color', '#000');
$('.non-initialized').caption(); // Error, jQuery doesn't know what caption is
$body.caption('done loading...');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为 jquery-chainable,jQuery 方法必须返回 jQuery 对象或支持所有 jQuery 方法的对象。您只需决定是否希望您的插件可链接其他 jQuery 方法,或者是否希望它返回您自己的数据。你不能两者兼得。选择一个。
在代码示例中,您可以定义多个插件方法:
.someplugin()
和.caption()
。 jQuery 没有一种方法来实现仅适用于一个特定 DOM 对象的 jQuery 插件方法。但是,使该方法可用于所有 jQuery 对象并没有什么坏处,并且您只能将其用于有意义的对象。我认为你可以使用这个:
如果两个
.caption()
调用之间应该存在某种联系,请进一步解释,因为我没有从你的问题中得出这一点。The be jquery-chainable, a jQuery method MUST return a jQuery object or an object that supports all jQuery methods. You simply have to decide whether you want your plugin to be chainable for other jQuery methods or whether you want it to return your own data. You can't have both. Pick one.
In your code examples, you could just define more than one plugin method,
.someplugin()
and.caption()
. jQuery does not have a means of implementing a jQuery plugin method that applies to one specific DOM object only. But, there is no harm in making the method available on all jQuery objects and you can only use it for the ones that it makes sense for.I think you could use this:
If there's supposed to be some connection between the two
.caption()
calls, please explain that further because I don't follow that from your question.