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());
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…