关于jquery的 selector Hierarchy

<div class="nights">
        <h5 class="biguns">
            <div class="num">4</div>
            Nights
        </h5>
</div>

如果我想获得4,应该如何用selector Hierarchy呢?

先来看一下

child selector

Description: Selects all direct child elements specified by "child" of elements specified by "parent".

  • version added: 1.0jQuery( "parent > child" )

    parent: Any valid selector.

    child: A selector to filter the child elements.

As a CSS selector, the child combinator is supported by all modern web browsers including Safari, Firefox, Opera, Chrome, and Internet Explorer 7 and above, but notably not by Internet Explorer versions 6 and below. However, in jQuery, this selector (along with all others) works across all supported browsers, including IE6.

The child combinator (E > F) can be thought of as a more specific form of the descendant combinator (E F) in that it selects only first-level descendants.

$("div.nights > h5 > div").text()

注意,>要求要选择直接的子对象,下面是不能用的

$("div.nights  > div").text()
因为div不是div.nights的直接的子对象


再来介绍另一种方法

descendant selector

Description: Selects all elements that are descendants of a given ancestor.

  • version added: 1.0jQuery( "ancestor descendant" )

    ancestor: Any valid selector.

    descendant: A selector to filter the descendant elements.

A descendant of an element could be a child, grandchild, great-grandchild, and so on, of that element.

注意他和child selector的区别,descendant selector可以用一个子,孙子,重孙子对象

比如下面的方法可以执行

$("div.nights  div").text()

也可以写成

$("div.nights  h5  div").text()
参考: http://api.jquery.com/descendant-selector/

http://api.jquery.com/child-selector/

http://stackoverflow.com/questions/3890458/jquery-nested-selector/3890469#3890469






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值