Jquery 从多个元素的属性构建数组
我有如下html:Jquery 从多个元素的属性构建数组,jquery,Jquery,我有如下html: <li class="select2-selection__choice" title="10" data-select2-id="22"> <span class="select2-selection__choice__remove" role="presentation">×</span> 10 </li> × 10 我需要找到这个类的每个li:select2-selection\uuu choice并且我
<li class="select2-selection__choice" title="10" data-select2-id="22">
<span class="select2-selection__choice__remove" role="presentation">×</span>
10
</li>
×
10
我需要找到这个类的每个li
:select2-selection\uuu choice
并且我需要title
属性的唯一值。可能吗
我可以有很多这样的
li
元素,但它们有不同的值。我需要数组中的每个值。1.创建一个空jQuery数组
2.迭代每个li
元素,使用
3.使用获取标题值,然后使用将其转换为整数,并使用将其推送到数组中
这样做:-
var title_array = [];
$('li.select2-selection__choice').each(function(){
title_array.push(parseInt($(this).attr('title')));
});
console.log(title_array);
工作片段:-
$(文档).ready(函数(){
var title_数组=[];
$('li.select2-selection\uu choice')。每个(函数(){
title_array.push(parseInt($(this.attr('title'));
});
console.log(title_数组);
});代码>
-
×10
-
×11
-
×12
使用MaP(),并考虑在标题上使用Trimes()。
let d=$('.select2-selection\uuuu-choice').map(函数(){
返回$(this.attr('title')/.trim(),以获得更好的结果
}).get();
控制台日志(d)代码>
×10
×10
×10
您为实现这一目标做了哪些尝试?试试@wenus,很高兴帮助你:):)。谢谢