使用选择器而不是迭代获取文本值数组? [英] Get array of text values using selector instead of iterating?
本文介绍了使用选择器而不是迭代获取文本值数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些类似的HTML:
I have some HTML like:
<ul id='foo'>
<span><a>hello 1</a></span>
<span><a>hello 2</a></span>
<span><a>hello 3</a></span>
</ul>
我想获得一个包含所有元素的文本值的数组:
I want to get an array of all the text values of the elements like:
var texts = [ 'hello 1', 'hello 2', 'hello 3' ];
我正在尝试遍历每个对象,jQuery是否有某种方法可以使用选择器仅将它们全部获取?
I'm trying to iterate over each one, is there some way in jQuery to just grab all of them using a selector?
推荐答案
您可以使用 .map()
这样:
You can do it using .map()
like this:
var myArray = $("#foo span a").map(function() {
return $(this).text();
}).get();
这篇关于使用选择器而不是迭代获取文本值数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文