jQuery插件默认 [英] jQuery plugin defaults

查看:64
本文介绍了jQuery插件默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在设置默认值后更改插件中的默认值.例如.

Is there a way to change the defaults within a plugin after they have been set. For example.

var defaults = {
 imageWidth : w,
 imageHeight : h
};

稍后在代码中,我想再次更改imageWidth值.

Later in the code I want to change the imageWidth value again.

推荐答案

也许取决于插件.如果看起来像这样:

Maybe, depends on the plugin. If it looks like this:

(function() {
    var defaults = { /* ... */ };
    // Rest of the plugin goes here.
})();

那您就不走运了.但是,相当多的插件将其默认值发布为:

Then you're out of luck. However, quite a few plugins publish their defaults as:

$.fn.plugin.defaults

其中plugin是插件名称.如果默认设置是可以这样公开访问的(或者如果您想破解该插件以将其公开),则可以随时更改默认设置.当然,新的默认值将不适用于现有实例.

Where plugin is the plugin name. If the defaults are publicly accessible like that (or if you want to hack the plugin to make them public), then you can change the defaults whenever you want. Of course, the new defaults won't apply to existing instances.

如果这是您自己的插件,那么我认为您的体系结构有些偏离. 全屏"与指定的宽度和高度"配置应该是该插件的选项,并将默认值保留为明智的设置(其中明智的"与往常一样取决于上下文).然后,您只需要这样说:

If this your own plugin then I think your architecture is a bit off. The "full screen" versus "specified width and height" configuration should be an option for the plugin and leave the default as something sensible (where "sensible" is, as always, context dependent). Then you'd just say this:

$(x).plugin({ fullscreen: true }); // Full screen
// or
$(y).plugin({ w: 11, h: 23 });     // Specific dimensions

您还可以使用$.fn.plugin.defaults技巧来允许根据需要更改明智的默认值.

You could also use the $.fn.plugin.defaults trick to allow the sensible defaults to be changed as needed.

这篇关于jQuery插件默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆