将jQuery对象/代码转换为插件
turn jQuery object/code into a plugin
所以我已经阅读了一些教程,我仍然困惑如何把我的特定标签系统变成一个可用的插件。
我用一堆函数声明了下面的对象:
var Tags = {};
Tags.Class = function() {
...
};
然后声明原型:
Tags.TaggingForm = new Tags.Class();
// Only takes text input fields for now!
Tags.TaggingForm.prototype = {
initialize: function(tag_field){
...
},
hideInputField: function() {
...
},
然后我有了实际的函数调用使用那个对象:
Tags.make_tagging_input = function (id) {
var
tagger = new Tags.TaggingForm(id);
...
}
我只是想知道如何把它变成一个插件:目前我这样称呼它:
window.g_gloab_var = Tags.make_tagging_input("#id");
这个框架一直被建议,但我不确定如何应用它:
//You need an anonymous function to wrap around your function to avoid conflict
(函数(美元){
//Attach this new method to jQuery
$.fn.extend({
//This is where you write your plugin's name
tags_input: function() {
//Iterate over the current set of matched elements
return this.each(function() {
//code to be inserted here
});
}
});
//pass jQuery to the function,
//So that we will able to use any valid Javascript variable name
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )
})(jQuery);
现在没有时间深入讨论这个,但是我最喜欢的插件创建样板是Stefan Gabos的
很好的评论,很容易理解。希望这对你有所帮助。当我有更多时间的时候我会再来看看,但希望这对现在有帮助。
你可以使用你的原始代码,并有一个插件,像这样:
(function($) {
// your original code here
$.fn.extend({
tags_input: function(options) {
//Iterate over the current set of matched elements
// that we would call via $(selector).tags_input(options);
// e.g. $('.tags_input').tags_input({a: true, b: "some string"});
return this.each(function() {
// call your plugin here,
// options can be an object of parameters to the plugin
// using your code and calling it on "this"
// e.g. Tags.make_tagging_input("#" + this.id);
});
}
});
})(jQuery);
相关文章:
- 将函数转换为jQuery插件
- Java脚本插件,用于将数字转换为特定于区域性的货币格式,并将具有特定货币格式的非格式化数字转换为普通数字
- 如何将此JavaScript转换为插件
- 将jQuery插件转换为指令
- 将jquery插件转换为指令angular
- 尝试将jQuery插件转换为纯javascript
- Backbone.js的Backbone.Modelbinding插件中是否有绑定的转换器参数
- jQuery SVG插件转换动画错误
- Firefox 插件中的 OnBeforeRequest URL 重定向(从 Chrome 扩展程序转换)
- 将字节数组转换为 XUL 插件中的输入流
- js/jquery/other library插件来转换HTML->XML
- 将jQuery插件转换为AngularJS指令 - 如何应用if/else
- 尝试将jquery插件转换为Angular Directive
- 将 jquery 插件转换为 Angular 指令
- 将 jQuery 插件转换为 es6 模块
- 在 Babel 中转换插件与语法插件
- 将 Javascript 函数转换为 jQuery 插件
- 单选按钮和行为有效,但是当将工作单选按钮转换为 icheck 插件时,我无法找出一个简单的行
- 免费的SVG转换插件-创建占位符元素
- Cordova Telerik Native转换插件