如何使用jQuery获取嵌套div中带static类的元素ID集合
提取动态生成元素的ID数组(jQuery实现)
直接用jQuery的.map()结合.get()就能快速得到目标数组:
const idArray = $('#data').find('.static').map(function() { return this.id; }).get();
说明
.map()遍历所有匹配的.static元素,逐个返回元素的id属性.get()将jQuery封装的结果转换为原生JS数组,最终得到[line-plot, bar-plot]这样的格式
如果偏好原生JS写法,也可以这么做:
const staticElements = document.querySelectorAll('#data .static'); const idArray = Array.from(staticElements).map(el => el.id);
内容的提问来源于stack exchange,提问作者Gnani Kim
相关产品推荐
相关产品推荐

