1 从集合中通过指定的序号获取元素
html:
0
1
2
3
4
5
6
7
JS
jQuery(function(){
$("p").eq(2).css("color","red");
$("p").eq(3).css("color","red");
})
2 获取指定条件一致和指定范围的元素
html:
0
1
2
3
4
5
6
7
JS
jQuery(function(){
$("p").filter('.center').css("color","red");
})
$(function(){
$("p").slice(5,7).css("color","yellow");
})
3 获取与条件表达式一致的元素
html:
0
1
2
3
4
5
6
7
js
jQuery(function(){
$("p").each(function(){
switch(true){
case $(this).is(".center"):
$(this).css("color","red");
break;
case $(this).is(".aa"):
$(this).css("color","yellow");
break;
}
})
})
4 获取元素的上一个元素和下一个元素
Html:
1号
2号
3号
4号
5号
6号
7号
js
//获取元素的下一个元素
jQuery(function(){
$("p").next(".yes").css("color","red");
})
//获取元素的上一个元素
jQuery(function(){
$("p").prev(".yes").css("color","red");
})
5 获取元素的父元素和子元素
html:
1号
2号
3号
4号
5号
6号
7号
js
//获取元素的父元素
jQuery(function(){
$("p").parent().css("color","red");
})
//获取元素的子元素
jQuery(function(){
$("#aa").children(".yes").css("color","red");
})