简体   繁体   English

如何向jQuery应用程序添加选项和习惯

[英]How to add options and customs to a jQuery application

I have made a jQuery application, which I want to provide some options to it so the user will changes some values based on their prefrecens. 我已经制作了一个jQuery应用程序,我想为其提供一些选项,以便用户根据自己的偏好来更改一些值。 So I have seen many plugins have some sort of options in the head tag that user can modify and they will be affected directly in the plugin. 因此,我已经看到许多插件的head标签中都有用户可以修改的某些选项,它们将直接在插件中受到影响。 Something like this: 像这样:

jQuery(function($) {
  $.MyApp({
    background : #000,
    foreground : red,
    blur : 10px
  });
});

So let's say I have a js file already that works fine, but I want to give accessibility to the user of my code to modify some default values. 假设我已经有一个可以正常工作的js文件,但是我想让代码用户可访问性来修改一些默认值。 How can I do this? 我怎样才能做到这一点? I have made a sample application that blurs the background image. 我制作了一个使背景图像模糊的示例应用程序。 JSFiddle JSFiddle

Can you provide some example based on my demo to see how such a structure can work and overwrite the default values? 您能否根据我的演示提供一些示例,以查看这种结构如何工作并覆盖默认值? Thanks. 谢谢。

For example this is how you can provide default, but overridable option in a jQuery plugin: 例如,这是您可以在jQuery插件中提供默认但可覆盖的选项的方法:

$.fn.MyApp=function(options){
    var defaults={
        background : "#000",
        foreground : "#fff",
        blur : "5px"
    };
    var options=$.extend(defaults,options);

    $(this).css("-webkit-filter", "blur(" + options.blur + ")");  
};

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

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