Jquery查找类型的第一个直接的孩子 [英] Jquery Find first direct child of a type

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

问题描述



我想像一下这个标记:

 < div id =mainDiv> 
< div id =otherDiv>
< p>材料< / p>
< / div>
< p>材料2< / p>
< p>材料3< / p>
< / div>

所以这里,我想要得到的是第二段第一段成为一个直接的孩子



如果使用jquery,我会执行类似 $('#mainDiv')的操作find('p:first'); 我会得到第一个div里面的段落。



我需要的是忽略嵌套的孩子,只采取第一个直接的。我应该怎么做?

解决方案

使用直接后代选择器>

  $('#mainDiv> p:first')
pre>

甚至 children()

  $('#mainDiv')。children('p')。first()


I am looking for a way to find the first direct child of an element, of a precise type.

Let's imagine this markup:

<div id="mainDiv">
    <div id="otherDiv">
        <p> Stuff </p>
    </div>
    <p> Stuff 2 </p>
    <p> Stuff 3 </p>
</div>

So here, what I want to get is "Stuff 2" the first paragraph to be a direct child.

If using jquery I do something like $('#mainDiv').find('p:first'); I will get the paragraph inside the first div.

What I need is to ignore nested childs and take only the first direct one. How should I do that?

解决方案

Use the direct descendant selector >

$('#mainDiv > p:first')

or even children()

$('#mainDiv').children('p').first()

这篇关于Jquery查找类型的第一个直接的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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