小编tfo*_*ter的帖子

在命名空间的jQuery插件中跨方法共享设置

我正在编写一个插件,并遵循jQuery文档的推荐做法http://docs.jquery.com/Plugins/Authoring,当涉及到命名空间和多种方法时.

我的init()负责使用$ .extend()合并默认和自定义设置但是我无法弄清楚如何在init()方法之外使这些选项可用.说使用调用并初始化我的插件

$("a").myplugin({debug:false}); 
Run Code Online (Sandbox Code Playgroud)

我怎样才能在以后调用时引用debug属性

$("a").myplugin("someMethod")?  
Run Code Online (Sandbox Code Playgroud)

一个粗略的例子是:

   (function( $ ){
        var methods = {
            init: function(customSettings) {
                var options = {
                    debug: true
                }
                return this.each(function () {
                   if (customSettings) {
                       $.extend(options, customSettings);
                   }
                });
            },
            someMethod: function() {
               if(options.debug) { // <-- How can I access options here?
                   // do something
               }
            }
         }
    })( jQuery );

    $.fn.myplugin = function (method) {
                if (methods[method]) {
                    return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
                }
                else if (typeof method === 'object' || …
Run Code Online (Sandbox Code Playgroud)

jquery plugins

8
推荐指数
1
解决办法
3604
查看次数

标签 统计

jquery ×1

plugins ×1