开发者

How to get first parent of span that is a <p> in jQuery?

As per this page says:

Find all parent elements开发者_StackOverflow社区 of each span that is a paragraph.

How to get the first one that matches?


$("span").parents("p:first")


If you mean by first parent the one that is closest to the element, you can use Traversing/closest:

$(selector).closest('p');


jQuery returns such results in an array. Have you tried using [0] after the query. $("span").parents("p")[0]. This would give the first parent.


$("span").parents("p :first")

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜