简体   繁体   English

如何从 DOM 元素和选择器的混合创建 JQuery 容器

[英]How to Create a JQuery Container from a Mixture of DOM Elements and Selectors

I have some DOM objects and object selectors.我有一些 DOM 对象和对象选择器。 Is there a way in JQuery to select the mixed set of both? JQuery 有没有办法选择两者的混合集? The following is what I have attempted:以下是我尝试过的:

// Note that in the real code, I don't know how el1 and el2 were obtained.
// They may have associated id fields, or they may not.
let el1 = document.getElementById('id1');
let el2 = $('#container > div > span').get();
let groupSelector = $([el1, el2, '#id3, div > span']);

I know that if all the elements had ids, or if I knew the original selectors, I could simply concatenate those into a comma-separated list.我知道如果所有元素都有 id,或者如果我知道原始选择器,我可以简单地将它们连接到一个逗号分隔的列表中。 However, not all of these elements have ids, and I don't know the original selectors.但是,并非所有这些元素都有 id,而且我不知道原始选择器。 How can I create a single JQuery container that includes the items indicated in the example code?如何创建包含示例代码中指示的项目的单个 JQuery 容器?

来自@Taplar 的评论:

let groupSelector = $('#id3, div > span').add([el1, el2]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM