gpt4 book ai didi

javascript - 获取数组元素的所有值

转载 作者:行者123 更新时间:2023-11-30 10:46:49 24 4
gpt4 key购买 nike

我有一个包含来自选择器的 HTML 元素的数组。看起来像这样:

var selection = $('.features_items');
// selection = [div.features_item, div.features_item];

div 看起来像这样:

<div class="features_item selected" value="2">

现在我想从数组中的 div 中获取所有值:[2,3]

最佳答案

您可以使用 jQuery.map一步将 jQuery 对象映射到数组:

var values = $.map(selection, function() {
return $(this).val();
}); // values is now an array equal in length to 'selection'

关于javascript - 获取数组元素的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7876345/

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