主要方法有两类:使用选择器或者遍历函数,并且可以混合使用。以获取id为test的div下的第一个或最后一个div子元素为例,详细说明如下:
1、使用选择器:
$("#test>div:first-child") // $("#test>div:first")
$("#test>div:last-child") // $("#test>div:last")
2、使用遍历函数:
$("#test").children().first()
$("#test").children().last()
3、混合使用
$("#test>div").first()
$("#test>div").last()
$("#test").children(":first") // $("#test").children(":first-child")
$("#test").children(":last") // $("#test").children(":last-child")