1、纯css 获取子元素
#test1>div {background-color:red;}
#test1 div {font-size:14px;}
#test1>div:first-child {color:#ccc;}
因1示例中为#test1下的子元素 #test1>div 余#test1 div 并没有区别。做如下示例:
立即学习“前端免费学习笔记(深入)”;
h1> strong {color:red;}
h1 strong {color:red;}
2、JQuery css 获取子元素
$(function(){
$("#button1").click(function(){
$("#test1>Div").html("姓名");
$("#test1>Div").next().html("张三"); //若注释这步,两个div都为“姓名”。
$("#test1 div").css("color","green")
$("#test1>div:first").css("font-size","24px");
});
});
3 、JQuery 获取子元素 find() 与children()
$(function(){
$('ul.level-1').children().css('border', '1px solid green');
$('ul.level-2').find('li').css('border', '1px solid red');
$("#p1").html("children li长度:"+$("ul.level-1").children("li").length);
$("#p2").html("find li长度:"+$("ul.level-1").find("li").length);
});
children和find的区别:children只会查找直接子集,而find会跨越层级查找,一直找到没有为止。
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号