版权声明: https://blog.csdn.net/qq_36797286/article/details/81810447
1. 选择每个 class 为 one 的 div 父元素下的第二个子元素
$("#btn1").click(function(){
//选取子元素, 需要在选择器前添加一个空格.
$("div.one :nth-child(2)").css("background", "#ffbbaa");
});
2. 选择每个 class 为 one 的 div 父元素下的第一个子元素
$("#btn2").click(function(){
$("div.one :first-child").css("background", "#ffbbaa");
});
3. 选择每个 class 为 one 的 div 父元素下的最后一个子元素
$("#btn3").click(function(){
$("div.one :last-child").css("background", "#ffbbaa");
});
4. 如果 class 为 one 的 div 父元素下仅仅只有一个子元素,那么选中这个子元素
$("#btn4").click(function(){
$("div.one :only-child").css("background", "#ffbbaa");
});