4.1祖先
parent():当前元素的父元素
$("#myself").parent().css("backgroundColor","red");
parents():所有的祖先元素
$(".myself").parents("[name = 'zhang']").css("backgroundColor","red")
注意:如果在小括号内传入参数,就可以找到你所指定的那一个祖先元素。
parentsuntil():选中两个指定元素中的祖先元素。
4.2后代
children():当前元素的直接后代
$(".myself").children(".firstSon").css("backgroundColor","red")
在括号中传入参数,找到的是指定的直接后代
children.children():直接后代的子元素
$("#myself").children().children().css("backgroundColor","black");
find():找到当前元素的所有直接后代
$(".myself").find("div").css("backgroundColor","red")
4.3同胞
next():找到弟弟元素
$("#myself").next().css("backgroundColor","yellow");
sibling:找到当前元素的所有同胞元素。
$(".myself").siblings().css("backgroundColor","red")
nextAll():找到所有的弟弟元素
$(".myself").nextAll().css("backgroundColor","red")
prev():找到哥哥元素
$("#myself").prev().css("backgroundColor","blue");
nextUntil():找到的是当前元素到指定元素之间的同胞元素。
$(".myself").nextUntil(".p6").css("backgroundColor","red")
prevAll():找到当前元素的所有哥哥元素
$(".p6").prevAll().css("backgroundColor","red")