jQuery向父輩遍歷的簡(jiǎn)單方法
通過(guò)DOM樹(shù)可以可容易的訪問(wèn)到html文檔中的所有元素
例如向上訪問(wèn)父輩的元素有以下方法
1.parent()方法可以得到所定元素的直接父元素
$("span").parent();得到<span>元素的直接父元素
2.parents()方法得到給定元素的所有父元素
$("span").parents();得到<span>元素的所有父元素
$("span").panents(".text");得到<span>元素的父元素中class="text"的元素
3.parentsUntil()方法得到兩個(gè)給定元素之間的元素
$("span").parentsUntil(".text");得到<span>元素與class="text"元素之間的所有元素
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"> </script> <style> .container { float:left; margin-left:30px; } .container div { border:1px solid grey; margin:15px auto; } .ancestor1-1,.ancestor2-1,.ancestor3-1,.ancestor4-1 { width:150px; height:150px; } .ancestor1-2,.ancestor2-2,.ancestor3-2,.ancestor4-2 { width:120px; height:120px; } .ancestor1-3,.ancestor2-3,.ancestor3-3,.ancestor4-3 { width:90px; height:90px; } .now1,.now2,.now3,.now4 { width:60px; height:60px; } </style> <script> $(document).ready(function(){ $(".now1").parent().css("border-color","red"); $(".now2").parents().css("border-color","red"); $(".now3").parents(".ancestor3-2").css("border-color","red"); $(".now4").parentsUntil(".ancestor4-1").css("border-color","red"); } ); </script> </head> <body> <div> <div class="container"> <div class="ancestor1-1"><div class="ancestor1-2"><div class="ancestor1-3"><div class="now1">給定元素</div></div></div></div> </div> <div class="container"> <div class="ancestor2-1"><div class="ancestor2-2"><div class="ancestor2-3"><div class="now2">給定元素</div></div></div></div> </div> <div class="container"> <div class="ancestor3-1"><div class="ancestor3-2"><div class="ancestor3-3"><div class="now3">給定元素</div></div></div></div> </div> <div class="container"> <div class="ancestor4-1"><div class="ancestor4-2"><div class="ancestor4-3"><div class="now4">給定元素</div></div></div></div> </div> </div> </body> </html>
效果圖:
以上這篇jQuery向父輩遍歷的簡(jiǎn)單方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Jquery1.9.1源碼分析系列(十五)動(dòng)畫(huà)處理之外篇
這篇文章主要介紹了Jquery1.9.1源碼分析系列(十五)動(dòng)畫(huà)處理之外篇 的相關(guān)資料,需要的朋友可以參考下2015-12-12jQuery實(shí)現(xiàn)監(jiān)聽(tīng)下拉框選中內(nèi)容發(fā)生改變操作示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)監(jiān)聽(tīng)下拉框選中內(nèi)容發(fā)生改變操作,結(jié)合實(shí)例形式分析了jQuery針對(duì)select選中觸發(fā)change事件相關(guān)操作技巧,需要的朋友可以參考下2018-07-07jquery ztree實(shí)現(xiàn)右鍵收藏功能
最近做項(xiàng)目需要使用ztree做一個(gè)右鍵收藏功能,下面小編給大家分享實(shí)例代碼,需要的朋友參考下吧2017-11-11jQuery實(shí)現(xiàn)動(dòng)態(tài)添加tr到table的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)添加tr到table的方法,涉及jQuery針對(duì)table表格元素的動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-12-12jQuery對(duì)象與DOM對(duì)象轉(zhuǎn)換方法詳解
這篇文章主要介紹了jQuery對(duì)象與DOM對(duì)象的轉(zhuǎn)換方法,結(jié)合實(shí)例形式分析了jQuery對(duì)象及DOM對(duì)象的作用與二者的相互轉(zhuǎn)換技巧,需要的朋友可以參考下2016-05-05jQuery擴(kuò)展方法實(shí)現(xiàn)Form表單與Json互相轉(zhuǎn)換的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了jQuery擴(kuò)展方法實(shí)現(xiàn)Form表單與Json互相轉(zhuǎn)換的相關(guān)知識(shí),并給大家介紹了jquery兩種擴(kuò)展方法,需要的朋友可以參考下2018-09-09jQuery實(shí)現(xiàn)的登錄浮動(dòng)框效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)的登錄浮動(dòng)框效果代碼,點(diǎn)擊登陸鏈接可彈出懸浮登錄框,涉及jQuery中show與hide方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09