jQuery找到第一个P [英] jquery finding the first P

查看:267
本文介绍了jQuery找到第一个P的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

$(content).filter("p:first").text();

其中内容=

<div>
<div>
<div>
<p>adsdas</p>
<p>dsa</p>
<p>dsa</p>
<p>dsa</p>
</div>
<p>&nbsp;</p>
<p>asdadsadsads</p>
</div><p><br></p><p><br></p><p>asd</p><p><br></p><p><br></p></div>

我想发生的是找到第一个PARAGRAPH标签,无论它是在子级的顶部还是底部,还是在div的上方.如何告诉jQuery一直找到第一个

标记,直到第一个匹配项?

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?

谢谢

推荐答案

您可以使用 .find() 取得像这样的任何后裔

You can use .find() to get any level decendant, like this

$(content).find("p:first").text();

如果它可能是最高级别,则不清楚,您需要执行 .filter() .find() ...,但是结果是按顺序排列的,因此仍然采用第一个,例如这个:

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();

这篇关于jQuery找到第一个P的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆