我正在从jQuery boilerplate创建一个jQuery插件。到目前为止,我在实施方面都取得了成功。
但是,我希望我的插件可以这样调用
$('selector').myplugin({data}) -- and have it return a chainable object
或
$.myplugin({data}) --and have it return something (string, object, array).
我读到了将this.each() ... return this
移到每个方法,但我得到了
TypeError: undefined is not a function (evaluating '$.myplugin()')
以下是我想要做的一个例子。
<input class="textIn" type="text" />
<input class="textOut" type="text" />
<script>
var input = $('.textIn').val();
//CASE #1
// will perform method named someMethod with an arg and return a value
var output = $.myPlugin(someMethod, input);
//CASE #2
// will go through each node with class 'textOut' and perform someMethod with an arg
$('.textOut').myPlugin(someMethod, input);
//CASE #3
// will go through each node with class 'textOut' and perform someOtherMethod
$('.textOut').myPlugin(someOtherMethod);
</script>
也许情况#1和#2不能合并,我将不得不重命名该方法,但我不确定。任何帮助都会很棒!