jquery finding the first P
I have the following:
$(content).filter("p:first").text();
where content =
<div>
<div>
<div>
<p>adsdas</p>
<p>dsa</p>
<p>dsa</p>
<p>dsa</p>
</div>
<p> </p>
<p>asdadsadsads</p>
</div><p><br></p><p><br></p><p>asd</p><p><br></p><p><br></p></div>
What I want to happen is to find the first开发者_运维问答 PARAGRAPH tag, whether it be at the top or down a child level or more of a div. How can I tell jQuery to find the first
tags all the way down until the first match?
Thanks
You can use .find()
to get any level decendant, like this
$(content).find("p:first").text();
If it may be the top level, it's not as clear, you need to do .filter()
and .find()
...but the results are in order, so still take the first, like this:
$(content).find("p").andSelf().filter("p:first").first().text();
There are lots of ways to do this. One simple way is like so:
$("p", content).first().text();
I'm assuming content is a variable containing the html. I created an example on jsFiddle... Have a look
精彩评论