<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #div1, #div3 { 400px; height: 300px; border: 3px solid orange; background: #33B2C7; float: left; } #head:after { content: ''; display: block; clear: both; overflow: hidden; } #div2, #div4 { 300px; height: 200px; border: 3px solid goldenrod; background: #004099; } p { 200px; height: 100px; border: 3px solid olivedrab; background: #0000FF; color: white; } .bd * { display: block; border: 2px solid gray; margin-top: 20x; } .fl { background: orangered; } </style> <script src="jquery-2.2.4.min.js"></script> <script> $(window).load(function() { /*---向下遍历---*/ //查找第一层级的子元素 $('#div1').children('p').css({ 'background': 'red' }); //查找所有的子元素 $('#div1').find('p').css({ 'background': 'red' }); /*------end-----*/ /*-------向上遍历-----*/ //次层级父元素 $('a').parent('#pId').css("background", 'orange'); //所有的父元素 $('a').parents().css("border", '1px solid orange'); //父元素直到某元素 // $('a').parentsUntil($('#div3')).html("parentUntil加的"); /*---------end-------*/ /*-----同级遍历-----*/ /* siblings ,next,nextuntil,nextAll,pre, preAll, preUntil*/ //所有同级 $('h4').siblings().text(function(i, old) { return old + "----siblings()添加的"; }); $('h4').next().text(function(i, old) { return old + "-----next()添加的"; }) $('h4').nextAll().text(function(i, old) { return old + "-----nextAll()添加的"; }) $('h3').nextUntil('h6', 'h5').text(function(i, old) { return old + "------nextUntil('h6', 'h5')添加的"; }) $('h3').prev().text(function(i, old) { return old + "-----prev()添加的"; }) $('h3').prevAll().text(function(i, old) { return old + "-------prevAll()添加的"; }) $('h4').prevUntil('h1', 'h3').text(function(i, old) { return old + "-----prevUntil('h1', 'h3')"; }) /*------------------*/ /*-----直接选择----*/ /*first last 【filter('p')传入选择器】 【eq(0)传入index】 【not 传入选择器]*/ var fl = $('.fl'); fl.first().html(function(i, old) { return old + "-----first() 添加的" }); fl.eq(2).html(function(i, old) { return old + "-----eq(2) 添加的"; }); var p = $('p'); p.filter('#xx').html(function(i, old) { return old + "-----filter('#xx')添加的"; }); p.not('#xx').html(function(i, old) { return old + "-----not('#xx') 添加的的"; }); }) </script> </head> <body> <div id="head"> <div id="div1">div1 <div id="div2">div2 <p> <a>Hello</a> </p> </div> <p><a>机构数量工具</a></p> </div> <div id="div3">div1 <div id="div4">div2 <p id="pId"> <a>Hello</a> </p> </div> </div> </div> <div class="bd"> <p>p</p> <h1>H1</h1> <h2>H2</h2> <h3>H3</h3> <h4>H4</h4> <h5>H5</h5> <h6>H6</h6> </div> <div class="fl"> <p>直接选元素1</p> </div> <div class="fl"> <p>直接选元素2</p> </div> <div class="fl"> <p id="xx">直接选元素3</p> </div> <div class="fl"> <p>直接选元素4</p> </div> </body> </html>