javascript - How to get text inside array? - Stack Overflow

I have this array:var arr= ['<p id="p1">Paragraph 1<p>', '<p

I have this array:

var arr  = ['<p id="p1">Paragraph 1<p>', '<p id="p2">Paragraph 2<p>', '<p id="p3">Paragraph 3<p>']

How can I get the text Paragraph 3 from this array?

I have this array:

var arr  = ['<p id="p1">Paragraph 1<p>', '<p id="p2">Paragraph 2<p>', '<p id="p3">Paragraph 3<p>']

How can I get the text Paragraph 3 from this array?

Share Improve this question asked Apr 22, 2013 at 16:39 user2308046user2308046 2
  • 1 If you know you want the text "Paragraph 3", why do you need to get it from the array? If you don't know that, then by what criteria are you looking for content? – Pointy Commented Apr 22, 2013 at 16:40
  • Begs the questions, why do you have an Array of HTML markup in the first place? What is ultimately going to be done with it? Why do you only need the text content? Is it tied to something else you're trying to do? – silly little me Commented Apr 22, 2013 at 17:02
Add a ment  | 

2 Answers 2

Reset to default 4

You can use:

$(arr[2]).prop('innerHTML');

or:

$(arr[2]).html();

Fiddle

You can strip the inner text by chopping up the string using the split() method.

<script>

var arr  = ['<p id="p1">Paragraph 1<p>', '<p id="p2">Paragraph 2<p>', '<p id="p3">Paragraph 3<p>']

/* Even works with closed <p> element */

var arr  = ['<p id="p1">Paragraph 1</p>', '<p id="p2">Paragraph 2</p>', '<p id="p3">Paragraph 3</p>']

alert(arr[2].split('>')[1].split('<')[0])
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745507931a4630636.html

相关推荐

  • javascript - How to get text inside array? - Stack Overflow

    I have this array:var arr= ['<p id="p1">Paragraph 1<p>', '<p

    23小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信