<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jquery_05_dom</title>
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
/*--------------查找-------------*/
/* 取到所有li的子标签
li的子标签是ul标签,取的是ul标签的html
*/
console.log("=children==" + $("li").children().html());
/* li的标签有两个,我只想取其中的div标签
只能取子标签
*/
console.log("=children==" + $("li").children("ul").html());
/* children:取的是子标签,参数是一个选择器 */
console.log("=children==" + $("#ul").children(".demo2").html());
/* find找元素的时候找的是孩子和孙子(所有的后代) */
console.log("=find==" + $("#ul").find("li").length);
/* next:后面后面一个弟弟 */
console.log("=next==" + $("#li1").next().length + "--->" + $("#li1").next().html());
/* next:后面后面所有弟弟 */
console.log("=nextAll==" + $("#li1").nextAll().length + "--->" + $("#li1").nextAll().html());
/* prev:往前找一个哥哥
prevAll:往前找所有哥哥
*/
/* parent:找的是父元素 */
console.log("=parent==" + $("#li1").parent().length + "--->" + $("#li1").parent().html());
/* 找的是所有的祖宗 */
console.log("=parent==" + $("#li1").parents().length + "--->" + $("#li1").parents().html());
$("#li1").parents().each(function()
{
console.info("==parents==each===" + $(this).html())
});
/* 祖宗有好多,取包含ul的 */
console.info("==parents==" + $("#li1").parents("ul").html())
/* */
console.info("==siblings==" + $("#li1").siblings().length);
/* 取到所有的哥哥和弟弟 */
$("#li1").siblings().each(function()
{
console.info("==siblings=each==" + $(this).html())
});
});
</script>
</head>
<body>
<ul id="ul">
<li>li_1</li>
<li id="li1">
li_2
<div>
这是div
</div>
<ul>
<li class="demo1">li_2_1</li>
<li>li_2_2</li>
</ul>
</li>
<li class="demo2">li_3</li>
<li class="demo2">li_4</li>
</ul>
</body>
</html>
|