查找兄弟元素

向下查找兄弟元素

  • 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. Selecting Courses POJ - 2239(我是沙雕吧 按时间点建边 || 匹配水题)

    呃呃呃呃呃 把每个课给了INF个容量....我是沙雕把....emm....这题就是做着玩...呃呃呃别当真.... #include <iostream> #include <cs ...

  2. 【BZOJ4944】【NOI2017】泳池 概率DP 常系数线性递推 特征多项式 多项式取模

    题目大意 有一个\(1001\times n\)的的网格,每个格子有\(q\)的概率是安全的,\(1-q\)的概率是危险的. 定义一个矩形是合法的当且仅当: 这个矩形中每个格子都是安全的 必须紧贴网格 ...

  3. MT【246】方程根$\backsim$图像交点

    已知函数$f(x)=x^2+x-2$,若$g(x)=|f(x)|-f(x)-2mx-2m^2$ 有三个不同的零点,则$m$的取值范围_____ 分析:等价于$h(x)=|f(x)|-f(x),t(x) ...

  4. MT【169】拉格朗日配方

    已知$x^2+y^2+z^2=1$求$3xy-3yz+2z^2$的最大值______ 答案:$3$ 提示:$3(x^2+y^2+z^2)-(3xy-3yz+2z^2)=3\left(y+\dfrac{ ...

  5. Windows server install mrtg

    由于MRTG使用Perl语言编写 , 安装ActivePerl http://downloads.activestate.com/ActivePerl/releases/5.20.1.2000/Act ...

  6. 【arc071f】Infinite Sequence(动态规划)

    [arc071f]Infinite Sequence(动态规划) 题面 atcoder 洛谷 题解 不难发现如果两个不为\(1\)的数连在一起,那么后面所有数都必须相等. 设\(f[i]\)表示\([ ...

  7. nginx日志分析 GoAccess

    也可以生成json:goaccess -q -f web.log -a -p /home/yejianfeng/.goaccessrc -o json >test.json 和csvgoacce ...

  8. HDU5985 Lucky Coins 概率dp

    题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...

  9. 通俗易懂的来理解Iaas,Paas,SaaS

    首先我们先来了解一下这几个单词的意思和完全的英文 Iaas:Infrastructure as a service    基础设施即服务 Paas:Platform as a service   平台 ...

  10. 关于windows下NODE_ENV=test无效的情况解决办法

    redux的单元测试命令为 NODE_ENV=test mocha --recursive --compilers js:babel-core/register --require ./test/se ...