查找兄弟元素

向下查找兄弟元素

  • next()
  • nextAll()
  • nextUntil()

向上查找兄弟元素

  • prev()
  • prevAll()
  • prevUntil()

查找所有兄弟元素

  • siblings()

 1.1.1.next()方法用来查找下一个兄弟元素,可以传参也可以不传参。参数可以是任意jQuery选择器,表示如果下一个元素如果是指定的元素就选定。当没有选中指定的元素时,jQuery链式调用还是保持原来的jQuery对象。

<!-- next -->
<button>点我</button>
<span class="demo">duyi-cst</span>
<p class="demo">duyi-cst</p>

html代码

  示例:

$('button').click(function(){
$(this).next().css({fontSize:'20px',color:'orange'});
console.log( $(this).next('span') );//可以选中元素span
console.log( $(this).next('p') );//不能选中元素p,jQuery链式调用还是保持button的jQuery对象
});

1.1.2nextAll()方法用来查找下面所有兄弟元素,可以传参也可以不传参。参数可以是任意jQuery选择器,表示如果同级后面的元素如果是指定的元素就选定。当没有选中指定元素时,jQuery链式调用还是保持原来的jQuery对象。

<div class="wrapper">
全选:<input type="checkbox"></input>
banana:<input type="checkbox" name="">
apple:<input type="checkbox" name="">
orange:<input type="checkbox" name="">
<input type="submit" value="login" name=""></input>
</div>

  示例:使用nextAll()通过全选按钮获取后面的所有复选框,并实现全选和全部撤销功能

$('input[type="checkbox"]').eq(0).click(function(){
if( $(this).prop('checked') ){
$(this).nextAll('input[type="checkbox"]').prop('checked',true);
}else{
$(this).nextAll('input[type="checkbox"]').prop('checked',false);
}
});

 1.1.3.nextUntil()方法用来查找下面所有兄弟元素,可以传入两个参数,第一个参数指定结束位置,第二个参数指定选择的元素;参数可以是任意jQuery选择器。

//html代码
<div class="wrapper1">
<h1>水果</h1>
全选:<input type="checkbox"></input>
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox">
<h1>nba</h1>
全选:<input type="checkbox"></input>
Rose:<input type="checkbox">
Curry:<input type="checkbox">
James:<input type="checkbox">
<input type="button" value="submit">
</div> //js代码
$('.wrapper1 h1').next().click(function(){
if( $(this).prop('checked') ){
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',true);
}else{
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',false);
}
});

1.2.1.prev()方法用来查找上一个兄弟元素,可以传参也可以不传参。参数可以是任意jQuery选择器,表示如果上一个元素如果是指定的元素就选定。当没有选中指定的元素时,jQuery链式调用还是保持原来的jQuery对象。

<span class="demo">duyi-cst</span>
<p class="demo">duyi-cst</p>
<button>点我</button>

html代码

  示例:

$(this).prev().css({fontSize:'20px',color:'blu'});
console.log( $(this).prev('p') );//可以选中元素p
console.log( $(this).prev('span') );//不能选中元素p,jQuery链式调用还是保持button的jQuery对象

1.2.2.prevAll()方法用来查找上面的所有兄弟元素,可以传参也可以不传参。参数可以是任意的jQuery选择器,表示如果同级前面的兄弟元素如果是被指定的元素就选定。当没有选定指定元素时,jQuery链式调用还是保持原来的jQuery对象。

//html
banana:<input type="checkbox" name="">
apple:<input type="checkbox" name="">
orange:<input type="checkbox" name="">
全选:<input type="checkbox"></input> //js
$('input[type="checkbox"]').eq(3).click(function(){
if( $(this).prop('checked') ){
alert();
$(this).prevAll('input[type="checkbox"]').prop('checked',true);
}else{
$(this).prevAll('input[type="checkbox"]').prop('checked',false);
}
});

prevAll与nextAll异曲同工

1.2.3.prevUntil()方法与nextUntil()方法的机制是一样的,nextUntil向下指定结束查找位置和查找元素,prevUntil向上指定结束查找位置和查找元素。

1.3.1.siblings()方法用来获取所有兄弟元素,也可以传入一个参数(jQuery选择器)作为筛选条件。

//html
<ul>
<li>1</li>
<li class="a">2</li>
<li>3</li>
<li>4</li>
<li class="a">5</li>
</ul> //js
$('ul li').eq(2)
.css('backgroundColor','red')
.siblings()
.css('backgroundColor','orange')
.end()
.siblings('.a')
.css('backgroundColor','yellow');

注:一个关于兄弟元素查找的小demo

//html代码
<div class="wrapper2">
all:<input type="checkbox"></input> <h1>吃货清单</h1>
all:<input type="checkbox"></input> <h2>水果</h2>
全选:<input type="checkbox">
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox">
<h2>蔬菜</h2>
全选:<input type="checkbox">
tomato:<input type="checkbox">
egg:<input type="checkbox">
potao:<input type="checkbox"> <h1>明星清单</h1>
all:<input type="checkbox" name=""> <h2>NBA</h2>
全选:<input type="checkbox">
Rose:<input type="checkbox">
Curry:<input type="checkbox">
James:<input type="checkbox">
</div> //js代码
//案例实现
$('.wrapper2 input[type="checkbox"]').eq(0).click(function(){
if( $(this).prop('checked') ){
$(this).nextAll().prop('checked',true);
}else{
$(this).nextAll().prop('checked',false);
}
});
$('.wrapper2 h1').next().click(function(){
if($(this).prop('checked')){
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',true);
}else{
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',false);
}
});
$('.wrapper2 h2').next().click(function(){
if($(this).prop('checked')){
$(this).nextUntil('h2','input[type="checkbox"]').prop('checked',true);
}else{
$(this).nextUntil('h2','input[type="checkbox"]').prop('checked',false);
} });

demo

查找父级元素

  • parent()
  • parents()
  • closest()
  • offsetParent()
  • slice()

 2.1.1.parent()--方法用来获取直接父级元素,可以传入参数(jQuery选择器)匹配指定父元素,如果不是指定的则不匹配,返回一个空的jQuery对象。

//div
<div class="shop" data-id='101'>
<p>basketball-nike</p>
<button>add</button>
</div> //js
console.log( $('button').parent() );//获取到div
console.log( $('button').parent('.aaa') );//没有获取到,返回空的jQuery对象

2.1.2.parents()--方法用来获取所有祖先元素,也可以传入参数(jQuery选择器)筛选,多个符合筛选条件的都会被匹配到。没有符合条件的,返回空的jQuery对象。

//html同上

//js
console.log( $('button').parents() );//div-body-html
console.log( $('button').parents('body') );//body
console.log( $('button').parents('.aaa') );//没有获取到,返回空的jQuery对象

2.2.1.closest()--方法用来获取符合条件的最近的一个父级元素,并且可以获取本身;不传入参数或参数没有匹配的祖先元素就返回一个空的jQuery对象;

console.log( $('.shop button').closest() );//空对象
console.log( $('.shop button').closest('.shop') );//div
console.log( $('.shop button').closest('button') );//自己本省
console.log( $('.shop button').closest('.aaa') );//空对象

2.2.2.offsetParent()--方法用来获取最近的有设定定位的父级元素。

//html
<div class="wrapper" style="position: relative">
<div class="box">
<span>123</span>
</div>
</div> //js
console.log( $('.wrapper span').offsetParent() );//选取到了<div class="wrapper" style="position: relative">

2.2.3slice()--通过指定索引范围来截取jQuery对象中DOM的部分,索引范围采用左闭右开,第一个索引的元素可以获取,第二个索引的元素不能获取。

//html
<ul>
<li>1</li>
<li class="a">2</li>
<li>3</li>
<li class="a">4</li>
<li class="a">5</li>
</ul> //js
console.log( $('ul li').slice(1,4) );//获取到索引为1,2,3的li元素

jQuery使用(四):DOM操作之查找兄弟元素和父级元素的更多相关文章

  1. Jquery遍历之获取子级元素、同级元素和父级元素

    Jquery遍历之获取子级元素.同级元素和父级元素 Jquery的遍历,其实就当前位置的元素相对于其他元素的位置的关系进行查找或选取HTML元素.以某项选择开始,并沿着这条线进行移动,或向上(父级). ...

  2. jQuery里面的DOM操作(查找,创建,添加,删除节点)

    一:创建元素节点(添加) 创建元素节点并且把节点作为元素的子节点添加到DOM树上 append(): 在元素下添加元素 用法:$("id").append("定义的节点& ...

  3. jquery学习笔记---Dom操作

    一.DOM操作的分类 DOM(document object model)是一种与浏览器.平台.语言无关的接口,使用该接口可以访问页面中的·所有组件.DOM的操作可以分为DOM Core.HTML-D ...

  4. 锋利的jQuery ——jQuery中的DOM操作(三)

    一.DOM的操作分类 1>DOM Core   2>HTML-DOM   3>CSS-DOM 二.jQuery中的DOM操作 DOM树 ①查找节点 1)查找元素节点 利用jQuery ...

  5. jQuery中的DOM操作------复制及包裹节点

    1.复制节点: 如果单击<li>元素后需要再复制一个<li>元素,可以用clone()方法来完成: $(this).clone().appendTo("ul" ...

  6. [jQuery]相对父级元素的fixed定位

    (function($) {     var DNG = {};     //----------------------------------------------------/     // ...

  7. jquery选择器:获取父级元素、同级元素、子元素

    jQuery的出现给广大开发者提供了不少的方便.从要自己一个一个敲代码,到直接调用方法,无疑大大地提高了网站开发的效率.而在jQuery中有一些方法非常的实用.下面就给大家介绍下jquery选择器:获 ...

  8. Jquery基础之DOM操作

    转自:http://www.cnblogs.com/bro-ma/p/3063942.html JQuery中的DOM操作主要对包括:建[新建].增[添加].删[删除].改[修改].查[查找][像数据 ...

  9. [转]jQuery 选择器和dom操作

    居然是12年的总结.... 文章地址: http://www.cnblogs.com/happyPawpaw/articles/2595092.html JQuery选择器 1.基本选择器 基本选择器 ...

随机推荐

  1. Codeforces518 D. Ilya and Escalator

    传送门:>Here< 题意:有n个人排队做电梯,每个人必须等前面的人全部上了以后才能上.对于每秒钟,有p的概率选择上电梯,(1-p)的概率选择不上电梯.现在问t秒期望多少人上电梯 解题思路 ...

  2. 【XSY2772】数列 特征多项式 数学

    题目描述 给你一个数列: \[ f_n=\begin{cases} a^n&1\leq n\leq k\\ \sum_{i=1}^k(a-1)f_{n-i}&n>k \end{c ...

  3. reactNative 基础

    参考:中文网,极客 一 . 基本程序: import React, { Component } from 'react'; import { Text } from 'react-native'; e ...

  4. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

  5. 【BZOJ1568】[JSOI2008]Blue Mary开公司(李超线段树)

    [BZOJ1568][JSOI2008]Blue Mary开公司(李超线段树) 题面 BZOJ 洛谷 题解 是模板题啊. #include<iostream> #include<cs ...

  6. Shiro中的Rememberme后出现浏览器500错误

    问题详述:在Shiro中添加Remember me功能后,只要勾选Remember me选项为true的时候,浏览器就会跳转到一个不可达页面,并且在Chrome中显示HTTP 500错误. 问题追踪: ...

  7. angularjs优化方略

    angular优化方略,闲的没事想重构的人来瞅瞅. 1.减少$watch 减少$watch,减少$watch,减少$watch.不仅仅是$watch监听,还有ng-model,别闲的没事就加个ng-m ...

  8. MySQL数据库的基本使用简单易懂

    MySQL数据库的基本使用 一.数据库概述 1. 基本介绍 数据库就是以一定格式进行组织的数据的集合.通俗来看数据库就是用户计算机上 一些具有特殊格式的数据文件的集合 2. 数据库的特点 持久化存储 ...

  9. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  10. bzoj3467: Crash和陶陶的游戏

    就一篇题解: BZOJ3467 : Crash和陶陶的游戏 - weixin_34248487的博客 - CSDN博客 1.离线,建出Atrie树:B树的倍增哈希数组,节点按照到根路径字典序排序 2. ...