JQuery遍历元素的后代和同胞实现方法

children() 方法返回被选元素的所有直接子元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> </style> <script type="text/javascript" src="https://www.jb51.net/jQuery/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $("#div1").children().each(function(i, e) { $("#info").html($("#info").html()+"第"+i+"个直接后代是,("+$(this).attr("id")+")"); }); }); }); </script> </head> <body> <input type="button" value="点击"><div></div> <div> <div> <div> <div> </div> </div> </div> <p></p> <p></p> <span></span> <span></span> <p></p> </div> </body> </html>

JQuery遍历元素的后代和同胞实现方法

find()

find() 方法返回被选元素的后代元素,一路向下直到最后一个后代。

find()里必须加上selecter 如果不加就显示不了

所以里面必须加选择器 例如find("*") find("span")

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> </style> <script type="text/javascript" src="https://www.jb51.net/jQuery/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $("#div1").find("*").each(function(i, e) { $("#info").html($("#info").html()+"第"+i+"个后代是,("+$(this).attr("id")+")"); }); }); }); </script> </head> <body> <input type="button" value="点击"><div></div> <div> <div> <div> <div> </div> </div> </div> <p></p> <p></p> <span></span> <span></span> <p></p> </div> </body> </html>

2.遍历同胞

siblings()

siblings() 方法返回被选元素的所有同胞元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> </style> <script type="text/javascript" src="https://www.jb51.net/jQuery/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $("#div2").siblings().each(function(i, e) { $("#info").html($("#info").html()+"第"+i+"个同胞是,("+$(this).attr("id")+")"); }); }); }); </script> </head> <body> <input type="button" value="点击"><div></div> <div> <div> <div> <div> </div> </div> </div> <p></p> <p></p> <span></span> <span></span> <p></p> </div> </body> </html>

next()

next()被选元素的下一个同胞元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> </style> <script type="text/javascript" src="https://www.jb51.net/jQuery/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $("#p2").next().each(function(i, e) { $("#info").html($("#info").html()+"第"+i+"个同胞是,("+$(this).attr("id")+")"); }); }); }); </script> </head> <body> <input type="button" value="点击"><div></div> <div> <div> <div> <div> </div> </div> </div> <p></p> <p></p> <span></span> <span></span> <p></p> </div> </body> </html>

nextAll()

nextAll() 方法返回被选元素的所有跟随的同胞元素。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjfjy.html