开发者

Plugin method declarations - can't make it work

I've been struggling with exactly what the correct syntax is to make methods available on an object with a plugin. Here's the basic framework:

<!DOCTYPE html>
<html>
<head>
<!-- jQuery -->
<script type="text/javascript" src="http://goo.gl/XQPhA"></script>
<script type="text/javascript">
(function($) {
    $.test = function(el, options) {
        this.whiten = function() {
            $(this).css('background-color', options.bg);
        }
    };
    $.test.settings = {
        bg: 'white'
    };
    $.fn.test = function(options) {
        options = $.extend(options, $.test.settings);
        return this.each(开发者_如何学编程function() {
            $.test(this, options);
        });
    };
})(jQuery);

$(document).ready(function() {
    $('#list').test().css('background-color', 'wheat');
    $('#go').click(function() {
        $('#list').whiten();
    });
});
</script>
</head>

<body>
<button id="go">whiten</button>
<ul id="list">
<li>Aloe</li>
<li>Bergamot</li>
<li>Calendula</li>
<li>Damiana</li>
<li>Elderflower</li>
<li>Feverfew</li>
</ul>
</body>
</html>

and I guess what I'm not sure about is how to make the function assignment. this inside of $.test will refer to the jQuery object wrapped around my list so I would have thought that this.myMethod = function() { would have worked but it doesn't. $(this) would be a double wrapper, el is my list (and I don't want to assign the method directly to the object since I wouldn't be able to call it like this: $('#list').whiten()), and $(el) would be the same as $(this)... so how is this done?

-- update --

I've created a [jsfiddle] to play with the problem

-- update --

I also did try placing the method in the $.fn.test function but to no avail


Try this:

 $.fn.test = function(options) {        
   options = $.extend(options, $.test.settings);      
   var self = this;  
   return this.each(function() {
        $.test(self, options);
    });
};


after much wailing and gnashing of teeth, I figured it out. I'm not sure I understand why it works that way but for now I'm just happy it does!

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://goo.gl/XQPhA"></script>
<script type="text/javascript">
(function($) {
    $.test = {
        bg: 'white'
    };

    $.fn.test = function(options) {
        options = $.extend({}, $.test, options);

        this.whiten = function() {
            $(this).css('background-color', options.bg);
        };

        return this.each(function() {
            $.fn.test(options);
        });
    };
})(jQuery);

$(document).ready(function() {
    $('#list').test().css('background-color', 'wheat');
    $('#go').click(function() {
        $('#list').whiten();
    });
});
</script>
</head>

<body>
<button id="go">whiten</button>
<ul id="list">
<li>Aloe</li>
<li>Bergamot</li>
<li>Calendula</li>
<li>Damiana</li>
<li>Elderflower</li>
<li>Feverfew</li>
</ul>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜