jquery兄弟标签_JQuery获取子孩子和兄弟元素的方法

这里说的获取当前元素的子孩子和兄弟元素的方法,另外还有遍历和指定的具体要求:

01.html代码:

测试jquery

This is div1

This is div_2

This id div2_1

This id div2_2

This id div2_3

This is div_3

01.css

body{

background-color: #efefef;

}

.div_1,.div_2,.div_2_1,.div_2_2,.div_2_3,.div_3{

position: absolute;

}

.div_1{

width: 200px;

height: 200px;

background-color: red;

}

.div_2{

width: 300px;

height: 700px;

background-color: yellow;

margin-left: 250px;

}

.div_2_1{

width: 200px;

height: 200px;

background-color: black;

margin: 10px 0px 0px 50px;

}

.div_2_2{

width: 200px;

height: 200px;

background-color: green;

margin: 220px 0px 0px 50px;

}

.div_2_3{

width: 200px;

height: 200px;

background-color: blue;

margin: 430px 0px 0px 50px;

}

.div_3{

width: 200px;

height: 200px;

background-color: white;

margin: 0px 0px 0px 600px;

}利用外部js文件的方式来处理如下:

01.js

$(document).ready(function(){

$("#button_1").click(function(){

//所谓的子孩子就是当前元素的子代

$('.div_2').children().each(function(){

window.alert($(this).text());

});

//下面是指定第几个子孩子

//alert($(".div_2").children().eq(0).text());

});

$("#button_2").click(function(){

//同辈元素的获取

//$('.div_1').nextAll().each(function(){

//window.alert($(this).text());

alert($(".div_2").nextAll().eq(0).text());

});

$("#button_3").click(function(){

//获取div_1前面的所有的兄弟元素

/*$(".div_3").prevAll().each(function(){

alert($(this).text());

});

*/

//获取.div的前面的第几个元素的内容

alert($(".div_2").prev().eq(0).text());

});

$("#button_4").click(function(){

$(".div_1").siblings().each(function(){

alert($(this).text());

});

});

});

以上是关于获取子元素和兄弟元素的具体方法,仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值