简体   繁体   English

jQuery 插件方法

[英]jQuery plugin methods

I am new to jQuery plugin creation and created a plugin for my project [Tumblr style tagging].我是 jQuery 插件创建的新手,并为我的项目 [Tumblr 样式标记] 创建了一个插件。

[I know there are so many plugins available but i want to create myself:D] [我知道有很多可用的插件,但我想自己创建:D]

here it goes就这样

(function($) {

    $.fn.Tagger = function () 
    {
        this.each(function()

        {
                //codes goes here

        });

    };


})(jQuery);

so that after doing this i can do something like this to create taggable input这样在这样做之后我可以做这样的事情来创建可标记的输入

$("#IdOfTheElement").Tagger();

Now i created the UI and need to get the value of the UI.现在我创建了 UI 并需要获取 UI 的值。

For example user types meta as google , yahoo , msn ....例如,用户将meta键入为googleyahoomsn ....

so that i need to get the values as google,yahoo,msn .所以我需要获得google,yahoo,msn的值。

i want to know the method of doing this [not the code]我想知道这样做的方法[不是代码]

something like this available?这样的东西可用吗?

$("#ID").Tagger("value"); //returns the values

or要么

$("#ID").Tagger().val(); // is this possible ?

Hope you understand the question,all comments/suggestions are welcome.希望你理解这个问题,欢迎所有评论/建议。

Please help me complete.请帮我完成。

Thank you.谢谢你。

Update更新

Please check this fiddle http://jsfiddle.net/pNqUL/请检查这个小提琴http://jsfiddle.net/pNqUL/

The last on you specified is possible by changing the code to this:您指定的最后一个可以通过将代码更改为此:

(function($) {

    $.fn.Tagger = function () 
    {
        return this.each(function()

        {
                //codes goes here

        });

    };


})(jQuery);

by returning the orignal selector match you are able to continue the daisy chain of commands.通过返回原始选择器匹配,您可以继续菊花链命令。 so if $('#ID') was a form field, then $("#ID").Tagger().val();所以如果$('#ID')是一个表单字段,那么$("#ID").Tagger().val(); would return the value会返回值

I collected all of my plugin's methods in an object literal and called them by passing the string name of the method to the plugin [as per the tutorial says...].我在 object 文字中收集了我所有插件的方法,并通过将方法的字符串名称传递给插件来调用它们 [按照教程所述...]。

so to apply the tagger i can do所以要应用我可以做的标记器

$("#ID").tagger(); // making element as taggable

$("#ID").tagger("value") //returns the value of tagged elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM