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

问题描述 投票:0回答:1

我有一些DOM对象和对象选择器。 JQuery中是否有办法选择两者的混合集?以下是我尝试过的内容:

// 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']);

我知道,如果所有元素都有ID,或者我知道原始的选择器,我可以将它们简单地连接成一个逗号分隔的列表。但是,并非所有这些元素都具有ID,并且我不知道原始的选择器。如何创建包含示例代码中指示的项目的单个JQuery容器?

javascript jquery jquery-selectors
1个回答
0
投票

[来自@Taplar的评论:

let groupSelector = $('#id3, div > span').add([el1, el2]);
© www.soinside.com 2019 - 2024. All rights reserved.