javascript – 带有选项和可访问方法的jQuery插件的模板?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 带有选项和可访问方法的jQuery插件的模板?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2633字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 带有选项和可访问方法的jQuery插件的模板?](/upload/InfoBanner/zyjiaocheng/785/46480fdace3e48cb8c7f132affd1c200.jpg)
我想构建一个带有可访问方法和选项的插件,这适用于复杂的插件.
我需要在插件外部访问这些方法,因为如果有人向DOM广告,则需要更新(因此我们不需要再次运行完整的插件).
我在过去看到有插件可以这样做,但我找不到它们,所以我不能看看它们.我仍然是javascript的新手,所以任何帮助都会很好.
如果我们仍然可以全局覆盖选项,那将是很好的.
我想如何使用插件:
// options
$('#someid').myplugin({name: 'hello world'});
// methods(would be nice if we can use this)
$('#someid').myplugin('update');
//我的旧插件包装器
;(function($, window, document, undefined){
$.fn.pluginmyPlugin = function(options) {
options = $.extend({}, $.fn.pluginmyPlugin.options, options);
return this.each(function() {
var obj = $(this);
// the code
});
};
/**
* Default settings(dont change).
* You can globally override these options
* by using $.fn.pluginName.key = 'value';
**/
$.fn.pluginmyPlugin.options = {
name: '',
...
};
})(jQuery, window, document);
更新
所以在查看了jQuery文档后,我构建了以下代码,请告诉我代码是否有问题,如果可以更好地构建…
;(function($, window, document, undefined){
var methods = {
init : function( options ) {
options = $.extend({}, $.fn.pluginmyPlugin.options, options);
return this.each(function(){
alert('yes i am the main code')
});
},
update : function( ) {
alert('updated')
}
};
$.fn.pluginmyPlugin = function(method) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on this plugin' );
}
};
/**
* Default settings(dont change).
* You can globally override these options
* by using $.fn.pluginName.key = 'value';
**/
$.fn.pluginmyPlugin.options = {
name: 'john doe',
//....
};
})(jQuery, window, document);
解决方法:
替代:
var Plugin = function($self, options) {
this.$self = $self;
this.options = $.extend({}, $.fn.plugin.defaults, options);
};
Plugin.prototype.display = function(){
console.debug("Plugin.display");
};
Plugin.prototype.update = function() {
console.debug("Plugin.update");
};
$.fn.plugin = function(option) {
var options = typeof option == "object" && option;
return this.each(function() {
var $this = $(this);
var $plugin = $this.data("plugin");
if(!$plugin) {
$plugin = new Plugin($this, options);
$this.data("plugin", $plugin);
}
if (typeof option == 'string') {
$plugin[option]();
} else {
$plugin.display();
}
});
};
$.fn.plugin.defaults = {
propname: "propdefault"
};
用法:
$("span").plugin({
propname: "propvalue"
});
$("span").plugin("update");
这荒谬地类似于Twitter Bootstrap’s JavaScript template.但是,它并没有完全从那里开始.我有使用.data()的悠久历史.
别忘了妥善包装.
内容总结
以上是互联网集市为您收集整理的javascript – 带有选项和可访问方法的jQuery插件的模板?全部内容,希望文章能够帮你解决javascript – 带有选项和可访问方法的jQuery插件的模板?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。