我尝试了一些常见的应用程序的具体行动移到jQuery插件由:什么是使JQuery插件可扩展的方法?
$.fn.extpoint = function() {...}
但我不想申报的几个扩展点:
$.fn.extpoint1 = function() {...}
$.fn.extpoint2 = function() {...}
...
相反,我想使用语法糖,如:
$("#id").extpoint.func1().extpoint.func2()
随着定义:
$.fn.extpoint = {}
$.fn.extpoint.func1 = function() {
this.val();
this.data("ip");
...
return this;
}
并调用:
$("#id").extpoint.func1(...)
this
指向$.fn.extpoint
(字典与func1
,func2
,...的元素),而不是原来的jQuery对象,当func1
评价。
是否有可能使jQuery插件可扩展?
PS。可以将函数名称作为第一个参数传递给$.fn.extpoint
,并实现$.fn.extpoint('extend', func)
调用来扩展(保存到名称和实现之间的内部字典关联)扩展点。在这种情况下,使用情况是这样的:
$("#id").extpoint('func1', ...).extpoint('func2', ...)
,但我期待的方式让更多的语法糖...
它不会以这种方式工作:'$( “#ID”)extpoint.func1()extpoint.func2()'因为FUNC1和FUNC2将不能访问'$(。 “#id”)' – 2013-03-13 15:14:47
@KevinB Yea,我明白了,但是想方设法避免这种情况,并且有一些未知的技巧...... +1 – gavenkoa 2013-03-13 17:44:06
一年或两年前,jQuery论坛上发布了一篇文章,其中有人想出了一个这样做的工作方式,但它非常复杂,我不记得它是如何完成的。我建议在那里搜索它,我找不到它。 – 2013-03-13 17:46:01