如何用jQuery对象构建jQuery对象 [英] How to build a jQuery object out of jQuery objects

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

问题描述

我有两个不同的jQuery对象,一个来自选择器查询,一个来自html代码.现在,我想从这些对象中创建另一个jQuery对象,以对它们两者进行操作.

I have two distinct jQuery objects, one from a selector query and one created from html code. Now I want to make another jQuery object out of these to do operations on both of them.

您可以在此处看到( http://jsfiddle.net/D799Y/)为此,请使用"get"操作并构建一个元素数组:

As you can see here (http://jsfiddle.net/D799Y/), it is possible to do this by using the "get" operation and building an array of elements:

var $first = $('div.first'),
    $overlay = $('<div class="overlay" />'),
    $list;

$first.before($overlay);

// build list out of elements
$list = $([ $first.get(0), $overlay.get(0) ]);

// modify the list
$list.css('border', '2px solid green');

是否有一种方法可以直接从两个jQuery对象中生成$ list?您可以将jQuery对象添加到另一个对象吗?

Is there a way to do make $list directly out of the two jQuery objects? Can you add a jQuery object to another one?

如果f.i. $first将包含多个元素.

This would also be more interesting if f.i. $first would contain more than one elements.

顺便说一句,我知道这种可能性,但它并不令人满意. :)

BTW, I know of this possibility, but it's so unsatisfactory. :)

$list = $(".first,.pane");

推荐答案

您可以简单地使用 add()方法:

You can simply use the add() method:

$list = $first.add($overlay);

请注意,add()是非破坏性的:它创建了一个新的jQuery对象,而不是修改您想要的对象.

Note that add() is non-destructive: it creates a new jQuery object instead of modifying the one it is called on, which is what you want.

这篇关于如何用jQuery对象构建jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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