如何在jQuery中的id下选择子元素 [英] How to select child elements under id in jQuery

查看:71
本文介绍了如何在jQuery中的id下选择子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

<div id='container'>
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>paragraph</p>
</div>



我想要一个jQuery等效的CSS选择器来选择 h1 div

#container h1 {}


推荐答案

完全相同的选择器:

You can simply implement the exact same selector:

$('#container h1')

它选择 #container 中找到的每个 h1 code>元素,或:

Which selects every h1 element found within the #container element, or:

$('#container > h1')

只选择那些元素的 h1 #container 元素作为其父级(不是祖父母或其他形式的祖先)。

Which selects only those h1 elements that have the #container element as their parent (not grandparent, or other form of ancestor).

偏好:

$('#container').find('h1')

您也可以将其限制为特定的 h1 元素:

You could also restrict it to a particular h1 element:

$('#container h1:first')


b $ b

只选择 #container 中的第一 h1 元素。

参考文献:

  • find().
  • :first selector.
  • jQuery selectors.

这篇关于如何在jQuery中的id下选择子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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