我嘗試了一些常見的應用程序的具體行動移到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