在jQuery中获取列表元素内容的数组 [英] Get an array of list element contents in jQuery
本文介绍了在jQuery中获取列表元素内容的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的结构:
<ul>
<li>text1</li>
<li>text2</li>
<li>text3</li>
</ul>
如何使用javascript或jQuery将文本作为数组获取?
How do I use javascript or jQuery to get the text as an array?
['text1', 'text2', 'text3']
我的计划是将它组装成一个字符串,可能使用 .join(',')
,然后在格式如下:
My plan after this is to assemble it into a string, probably using .join(', ')
, and get it in a format like this:
'"text1", "text2", "text3"'
推荐答案
var optionTexts = [];
$("ul li").each(function() { optionTexts.push($(this).text()) });
...应该做的伎俩。要获得您正在寻找的最终输出, join()
加上一些连接将很好地做到:
...should do the trick. To get the final output you're looking for, join()
plus some concatenation will do nicely:
var quotedCSV = '"' + optionTexts.join('", "') + '"';
这篇关于在jQuery中获取列表元素内容的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文