开发者

Aggregating a jQuery object?

I'm authoring a plugin, and the plugin needs to do something like aggregate a set of jQuery objects. How does one do this?

For example:

<p><a>...</a></p>
<p><a>...</a></p>

With

(function( $ )
{
    $.fn.myfunc = function( settings )
    {

    };
})(jQuery);

Within the context of the plugin invoked with $('p').myfunc(), how would I return all the elements, for example? The elements I'm returning will not necessarily be contained or near the elements selected, as this i开发者_开发知识库s just an example.


jQuery also accepts an array, so you can build your own node stack and create a jQuery object out of it.

Example:

(function( $ )
{
    $.fn.myfunc = function( settings )
    {
        var stack = [];
        stack.concat(this.find('a').toArray());
        stack.concat($('a.hot-links').toArray());
        return $($.unique(stack));
    };
})(jQuery);

Or simply:

return this.find('a'); // as return result of plugin

Also, look at .pushStack(), which lets you add elements to an already existing jQuery object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜