【问题标题】:How to Create a JQuery Container from a Mixture of DOM Elements and Selectors如何从 DOM 元素和选择器的混合中创建 JQuery 容器
【发布时间】:2020-05-15 07:42:49
【问题描述】:

我有一些 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】:

来自@Taplar 的评论:

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

【讨论】:

    猜你喜欢
    • 2012-05-22
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多