jQuery常見的遍歷DOM操作詳解
本文實例總結(jié)了jQuery常見的遍歷DOM操作。分享給大家供大家參考,具體如下:
向上遍歷DOM樹
- .parent():返回被選元素的直接父元素,該方法只會向上一級對DOM樹進行遍歷
- .parents():返回被選元素的所有祖先元素,一直向上遍歷,直到文檔的根元素(html)
- .parentsUntil():返回介于兩個給定元素之間的所有祖先元素
<!DOCTYPE html> <html> <head> <style> .ancestors *{ display:block; border:2px solid lightgrey; color:lightgrey; padding:5px; margin:15px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script > $(document).ready(function(){ $("span").parent().css({"color":"red","border":"2px solid red"}); }); </script> </head> <body> <div class="ancestors"> <div style="width:500;">div(曾祖父) <ul>ul(祖父) <li>li(直接父) <span>span</span> </li> </ul> </div> <div style="width:500px;">div(祖父) <p>p(直接父) <span>span</span> </p> </div> </div> </body> </html>
運行結(jié)果:
parentsUntil()
方法
$(document).ready(function(){ $("span").parentsUntil("div"); });
<!DOCTYPE html> <html> <head> <style> .ancestors *{ display:block; border:2px solid lightgrey; color:lightgrey; padding:5px; margin:15px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("span").parentsUntil("div").css({"color":"red","border":"2px solid red"}); }); </script> </head> <body class="ancestors">body(增曾祖父) <div style="width:500px;">div(曾祖父) <ul>ul(祖父) <li>li(直接父) <span>span</span> </li> </ul> </div> </body> </html>
運行結(jié)果:
向下遍歷DOM樹
- .children():返回被選元素的所有直接子元素,該方法只會向下一級對DOM樹進行遍歷
- .find():返回被選元素的后代元素,一直向下直到最后一個后代
children()
方法
<!DOCTYPE html> <html> <head> <style> .descendants *{ display:block; border:2px solid lightgrey; color:lightgrey; padding:5px; margin:15px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children().css({"color":"red","border":"2px solid red"}); $("div").children("p.1").css({"color":"red","border":"2px solid red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div(當前元素) <p class="1">p(子) <span>span(孫)</span> </p> <p class="2">p(子) <span>span(孫)</span> </p> </div> </body> </html>
運行結(jié)果:
find()
方法
<!DOCTYPE html> <html> <head> <style> .descendants *{ display:block; border:2px solid lightgrey; color:lightgrey; padding:5px; margin:15px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").find("span").css({"color":"red","border":"2px solid red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div(current element) <p>P子 <span>span(孫)</span> </p> <p>p子 <span>span(孫)</span> </p> </div> </body> </html>
運行結(jié)果:
返回<div>
所有后代
$(document).ready(function(){ $("div").find("*"); });
水平遍歷DOM樹
- .siblings():返回被選元素的所有同胞
- .next():返回被選元素下一個同胞元素
- .nextAll():返回被選元素的所有跟隨的同胞元素
- .nextUntil():返回介于兩個給定參數(shù)之間的所有跟隨的同胞元素
- .prev():返回被選元素上一個同胞元素
- .prevAll():返回被選元素的所有之前的同胞元素
- .prevUntil():返回介于兩個給定參數(shù)之間的所有之前的同胞元素
<!DOCTYPE html> <html> <head> <style> .siblings *{ display:block; border:2px solid lightgrey; color:lightgrey; padding:5px; margin:15px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("h2").siblings().css({"color":"red","border":"2px solid red"}); }); </script> </head> <body class="siblings"> <div>div(父) <p>p</p> <span>span</span> <h2>h2</h2> <h3>h3</h3> <p>p</p> </div> </body> </html>
運行結(jié)果:
jQuery遍歷 過濾
- first()方法:返回被選元素的首個元素
- last()方法:返回被選元素的最后一個元素
- eq()方法:返回被選元素中帶有指定索引號的元素
- filter()方法:允許自己規(guī)定一個標準,不匹配這個標準的元素會被從集合中刪除,匹配的元素會被返回。
- not()方法:返回不匹配的所有元素
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("div p").first().css("background-color","yellow"); }); </script> </head> <body> <h1>我心在北朝、</h1> <div> <p>田野上</p> </div> <div> <p>紅彤彤的野花</p> </div> <p>玲瓏剔透</p> </body> </html>
運行結(jié)果:
eq()
方法的使用
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").eq(1).css("background-color","yellow"); }); </script> </head> <body> <h1>我心在南朝、</h1> <p>田野上</p> <p>紅彤彤的野花</p> <p>玲瓏剔透</p> <p>我愛你</p> </body> </html>
運行結(jié)果:
filter()
方法的使用
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").filter(".intro").css("background-color","yellow"); }); </script> </head> <body> <h1>我心在南朝、</p> <p>田野上</p> <p class="intro">紅彤彤的草莓</p> <p class="intro">玲玲剔透</p> <p>我愛你</p> </body> </html>
運行結(jié)果:
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容還可查看本站專題:《jQuery操作DOM節(jié)點方法總結(jié)》、《jQuery遍歷算法與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
JQuery 簡便實現(xiàn)頁面元素數(shù)據(jù)驗證功能
JQuery 簡便實現(xiàn)頁面元素數(shù)據(jù)驗證功能...2007-03-03jQuery)擴展jQuery系列之一 模擬alert,confirm(一)
很多人都為了使alert系統(tǒng)的調(diào)用函數(shù)在自己的控制范圍之內(nèi),都選擇了去封裝一個屬于自己的alert組件,現(xiàn)在我們就動手實現(xiàn)一個這樣的小部件。2010-12-12Jquery ajax請求導出Excel表格的實現(xiàn)代碼
下面小編就為大家?guī)硪黄狫query ajax請求導出Excel表格的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06jquery ajax結(jié)合thinkphp的getjson實現(xiàn)跨域的方法
這篇文章主要介紹了jquery ajax結(jié)合thinkphp的getjson實現(xiàn)跨域的方法,結(jié)合實例形式對比分析了jQuery ajax實現(xiàn)跨域的具體操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06jQuery實現(xiàn)鼠標經(jīng)過購物車出現(xiàn)下拉框代碼(推薦)
在做web前端項目開發(fā)的時候,使用jquery庫的感觸頗多,下面小編通過寫購物車的下拉框做法,把我的想法給大家分享一下,感興趣的朋友可以參考下2016-07-07