开发者

Trying to get a string from within a <p> using jQuery

I am trying to get the string Test123 in the following with jQuery:

<div id="pri_txt"><p>Test123</p></div>

I used:

var str = $('#pri_txt')开发者_运维知识库.next('p').text();

But it doesn't seem to work. Am I missing something?


Close. next() will select the elements next sibling. What you're looking for is its child p tag. Use the descendant selector jQuery('ancestor descendant')

Selects all elements that are descendants of a given ancestor.

var str = $('#pri_txt p').text();

If you're looking for the first paragraph, use the :first selector

var str = $('#pri_txt p:first').text();

If you're searching for a specific paragraph, use the :eq(index) selector. Where index is the 0-based index of the element you're looking for in relation to the parent:

var str = $('#pri_txt p:eq(10)').text();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜