JQuery遍歷元素的父輩和祖先的方法
JQuery遍歷
首先我們要知道 什么是 父親,兒子,后代,同胞,祖先

•<div> 元素是 <ul> 的父元素,同時(shí)是其中所有內(nèi)容的祖先。
•<ul> 元素是 <li> 元素的父元素,同時(shí)是 <div> 的子元素
•左邊的 <li> 元素是 <span> 的父元素,<ul> 的子元素,同時(shí)是 <div> 的后代。
•<span> 元素是 <li> 的子元素,同時(shí)是 <ul> 和 <div> 的后代。
•兩個(gè) <li> 元素是同胞(擁有相同的父元素)。
•右邊的 <li> 元素是 <b> 的父元素,<ul> 的子元素,同時(shí)是 <div> 的后代。
•<b> 元素是右邊的 <li> 的子元素,同時(shí)是 <ul> 和 <div> 的后代。
父類(lèi)和祖先的遍歷
1.parent()
遍歷直接父親 不往上遍歷其它的祖先
<!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>無(wú)標(biāo)題文檔</title>
<style type="text/css">
</style>
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$("#info").html("div4的父親"+$("#div4").parent().attr("id"));
});
});
</script>
</head>
<body>
<input type="button" value="點(diǎn)擊" id="btn"><div id="info"></div>
<div id="div1">
<div id="div2">
<div id="div3">
<div id="div4">
</div>
</div>
</div>
</div>
</body>
</html>

2.parents()
遍歷它的所有祖先
.each(function(i,e){})
對(duì)其中的每個(gè)元素進(jìn)行遍歷
其中i是索引,e是當(dāng)前對(duì)象,相當(dāng)于$(this),但是前者是js對(duì)象,后者是jquery對(duì)象。
<!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>無(wú)標(biāo)題文檔</title>
<style type="text/css">
</style>
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$("#div4").parents().each(function(i, e) {
$("#info").html($("#info").html()+"第"+i+"個(gè)祖先是,("+$(this).attr("id")+")");
});
});
});
</script>
</head>
<body>
<input type="button" value="點(diǎn)擊" id="btn"><div id="info"></div>
<div id="div1">
<div id="div2">
<div id="div3">
<div id="div4">
</div>
</div>
</div>
</div>
</body>
</html>

那你就會(huì)問(wèn)了 第三個(gè)和第四個(gè)祖先是什么鬼 讓我們?cè)诰W(wǎng)頁(yè)中按F12調(diào)試一下 看看結(jié)果
當(dāng)i=3 也就是到了第三個(gè)祖先 結(jié)果是body
當(dāng)i=4 也就是到了第四個(gè)祖先 結(jié)果是html

3.parentsUntil()
遍歷到指定的祖先(不包括該祖先)
<!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>無(wú)標(biāo)題文檔</title>
<style type="text/css">
</style>
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$("#div4").parentsUntil("#div1").each(function(i, e) {
$("#info").html($("#info").html()+"第"+i+"個(gè)祖先是,("+$(this).attr("id")+")");
});
});
});
</script>
</head>
<body>
<input type="button" value="點(diǎn)擊" id="btn"><div id="info"></div>
<div id="div1">
<div id="div2">
<div id="div3">
<div id="div4">
</div>
</div>
</div>
</div>
</body>
</html>

所以范圍是在#div4和#div1中間 不包含#div1
以上這篇JQuery遍歷元素的父輩和祖先的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery3.0中的buildFragment私有函數(shù)詳解
在 jQuery3.0中,buildFragment 是一個(gè)私有函數(shù),用來(lái)構(gòu)建一個(gè)包含子節(jié)點(diǎn) fragment 對(duì)象。下文給大家介紹jQuery3.0中的buildFragment私有函數(shù)詳解,對(duì)jquery3.0 buildfragment相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-08-08
jQuery 自動(dòng)增長(zhǎng)的文本輸入框?qū)崿F(xiàn)代碼
文本輸入框內(nèi)的字?jǐn)?shù)不能確定,而input type="text"的size是固定的,當(dāng)字?jǐn)?shù)超過(guò)size時(shí)(默認(rèn)是20),先輸入的內(nèi)容就會(huì)從文本框的左端隱藏起來(lái),不便于輸入。2010-04-04
jQuery對(duì)象的selector屬性用法實(shí)例
這篇文章主要介紹了jQuery對(duì)象的selector屬性用法,實(shí)例分析了selector屬性返回選擇器的使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
jQuery實(shí)現(xiàn)的自動(dòng)加載頁(yè)面功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的自動(dòng)加載頁(yè)面功能,結(jié)合簡(jiǎn)單實(shí)例形式分析了jQuery針對(duì)頁(yè)面元素的動(dòng)態(tài)加載與屬性操作相關(guān)技巧,需要的朋友可以參考下2016-09-09

