JQ节点关系 children()、find()、parent()/parents()、siblings()以及next()、prev()

一、children()

  1、描述:获取当前jq对象的子节点

  2、语法:jq对象.children()

  3、PS:

      ①当children()不写参数的时候 获取的是所有的孩子

      ②当children('孩子的选择器') 获取的是指定的孩子

      ③children() 只能选择 直接孩子 不能选择 孙子元素

   4、eg:

$('button:first').click(function () {
     //选择指定的孩子
     $('.box').children('.s1').css('background-color','red');
     //选择所有的孩子
     $('.box').children().css('background-color','red');
     })

 二、find()

   1、描述:获取指定的孩子

   2、语法:jq对象.find('孩子的选择器')

   3、PS:

      ①不能空参使用,不能获取所有的孩子

      ②只要是孩子元素,就能使用find()

   4、eg:

 $('button:first').click(function () {
     console.log('666');
     $('.box').find('.s2').css('backgroundColor','red');
     });

三、parent()/parents()

   1、描述:查找当前jq对象的父节点/所有的父节点直到祖先节点

   2、语法:jq对象.parent()/parents()

   3、eg:

 $('button:first').click(function () {
     //$('.p0').parent().css('background-color','red');
     $('.p0').parents().css('background-color','red');
     });

四、siblings()

   1、描述:获取所有的兄弟节点

   2、语法:jq对象.siblings(选择器)

   3、PS:

        ①不写参数,获取的是所有的兄弟

        ②写参数,获取的是满足选择器要求的兄弟

   4、eg:

 $('button:first').click(function () {
       $('.p0').siblings('.p1').css('background-color', 'red');
 });

 $('p').click(function () {
       $(this).html('很开心').siblings().html('很开心').parent().siblings().children().html('不开心');
  });

五、next()

   1、描述:获取下一个兄弟节点

   2、语法:jq对象.next()

   3、eg:

  $('p').click(function () {
     $(this).next().css('background-color','red');
  })

六、nextAll()

  1、描述:获取后面所有的兄弟节点

   2、语法:jq对象.nextAll()

   3、eg:

 $('p').click(function () {
     $(this).nextAll().css('background-color', 'red');
 });

七、prev()

   1、描述:获取当前节点的前一个兄弟节点

   2、语法:jq对象.prev()

   3、eg:

$('p').click(function () {
     $(this).prev().css('background-color', 'red');
});

八、prevAll()

   1、描述:获取当前节点前面的所有兄弟节点

   2、语法:jq对象.prevAll()

   3、eg:

$('p').click(function () {
    $(this).prevAll().css('background-color', 'red');
});

 

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值