最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add jQuery objects to the jQuery composite object - Stack Overflow

programmeradmin4浏览0评论

Simply put: the jQuery object is a posite pattern. How do I add jQuery objects to it?

An example:

var e1 = $('#element1');
var e2 = $('#element2');

...

I know want to create a new jQuery object jq such that it is posed of e1 and e2. I want to be able to do something like the following:

var jq = $();
jq.addInjQueryObjects([e1, e2]);
jq.hide();

how do I do this?

By the way, I realize that I could have just selected #element1 AND #element2 to begin with, that's not what I'm talking about.

Simply put: the jQuery object is a posite pattern. How do I add jQuery objects to it?

An example:

var e1 = $('#element1');
var e2 = $('#element2');

...

I know want to create a new jQuery object jq such that it is posed of e1 and e2. I want to be able to do something like the following:

var jq = $();
jq.addInjQueryObjects([e1, e2]);
jq.hide();

how do I do this?

By the way, I realize that I could have just selected #element1 AND #element2 to begin with, that's not what I'm talking about.

Share Improve this question asked Oct 17, 2010 at 18:03 George MauerGeorge Mauer 122k140 gold badges396 silver badges630 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use jQuery's .add() method:

var e1 = $('#element1');
var e2 = $('#element2');

var jq = $();
jq = jq.add(e1).add(e2);
jq.hide();

It returns a new jQuery object instead of modifying the original, so you need to overwrite the original if you want to reuse the same variable.


EDIT: Note that you can also use jQuery.merge(), which will modify the original, but you'll need to pass in an Array of the DOM elements instead of the jQuery objects.

var e1 = $('#element1');
var e2 = $('#element2');

var jq = $();
$.merge( jq, [e1[0], e2[0]] );
jq.hide();
​
发布评论

评论列表(0)

  1. 暂无评论