gpt4 book ai didi

jquery - 查找并收集具有 `data-id` 属性的所有元素的值

转载 作者:行者123 更新时间:2023-12-01 07:39:31 24 4
gpt4 key购买 nike

如何查找所有具有 data-id 属性的元素并使用 map 函数收集值。

var target = $('#parent');
var arr = target.find('all elements having data-id attribute');
var cnt = $.map(arr, (e) => $(e).data('id')).join(',');
console.log(cnt);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='parent'>
<div class='title' data-id = 5>lorem</div>
<input type = 'text'>
<a href='google.com' data-id = 14>google</a>
<div class='title'>ipsum</div>
<img class='imgtop' src='' alt='img' data-id = 9>
</div>

结果应为5,14,9

有什么帮助吗?

最佳答案

这很简单

const arr = target.find('[data-id]')

参见https://api.jquery.com/has-attribute-selector/

关于jquery - 查找并收集具有 `data-id` 属性的所有元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com