聚集一个jQuery对象? [英] Aggregating a jQuery object?

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

问题描述

我正在编写一个插件,该插件需要执行一些操作,例如聚合一组jQuery对象.如何做到这一点?

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

例如:

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

使用

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

    };
})(jQuery);

在用$('p').myfunc()调用的插件的上下文中,例如,我将如何返回所有元素?我返回的元素不一定包含在所选元素中或附近,因为这只是一个例子.

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 is just an example.

推荐答案

jQuery还接受一个数组,因此您可以构建自己的节点堆栈并从中创建jQuery对象.

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

示例:

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

或者简单地:

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

此外,请查看 .pushStack(),它可以将元素添加到已经存在的元素中jQuery对象.

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

这篇关于聚集一个jQuery对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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