如何使用jQuery将所有元素的含量阵列? [英] How to put all elements' content in array using jQuery ?
本文介绍了如何使用jQuery将所有元素的含量阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< DIV ID =主>
&所述p为H.;文本1&下; / P>
&所述p为H.;文本2&下; / P>
&所述p为H.;文本3&下; / P>
< / DI>
结果应该是:
[text1中,文本2,text3中]
解决方案
jQuery提供 .MAP()
此:
VAR项目= $('#主P')图(函数(){$返回(本)的.text();})。获得();
.MAP()
迭代它的元素,调用他们每个人的功能,并在一个新的数组,它返回记录函数的返回值。
您也可以使用已经解决了这个简单的。每()
:
VAR项目= [];$('#主P')。每个(函数(I,E){
items.push($(E)的.text());
});
<div id="main">
<p>Text1</p>
<p>Text2</p>
<p>Text3</p>
</di>
Result should be :
["text1","text2","text3"]
解决方案
jQuery provides .map()
for this:
var items = $('#main p').map(function () { return $(this).text(); }).get();
.map()
iterates over its elements, invoking a function on each of them and recording the return value of the function in a new array, which it returns.
You could also have solved this with a simple .each()
:
var items = [];
$('#main p').each(function (i, e) {
items.push($(e).text());
});
这篇关于如何使用jQuery将所有元素的含量阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文