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();
精彩评论