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

•<div> 元素是 <ul> 的父元素,同時是其中所有內(nèi)容的祖先。
•<ul> 元素是 <li> 元素的父元素,同時是 <div> 的子元素
•左邊的 <li> 元素是 <span> 的父元素,<ul> 的子元素,同時是 <div> 的后代。
•<span> 元素是 <li> 的子元素,同時是 <ul> 和 <div> 的后代。
•兩個 <li> 元素是同胞(擁有相同的父元素)。
•右邊的 <li> 元素是 <b> 的父元素,<ul> 的子元素,同時是 <div> 的后代。
•<b> 元素是右邊的 <li> 的子元素,同時是 <ul> 和 <div> 的后代。
父類和祖先的遍歷
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>無標(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="點擊" 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){})
對其中的每個元素進(jìn)行遍歷
其中i是索引,e是當(dāng)前對象,相當(dāng)于$(this),但是前者是js對象,后者是jquery對象。
<!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>無標(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+"個祖先是,("+$(this).attr("id")+")");
});
});
});
</script>
</head>
<body>
<input type="button" value="點擊" id="btn"><div id="info"></div>
<div id="div1">
<div id="div2">
<div id="div3">
<div id="div4">
</div>
</div>
</div>
</div>
</body>
</html>

那你就會問了 第三個和第四個祖先是什么鬼 讓我們在網(wǎng)頁中按F12調(diào)試一下 看看結(jié)果
當(dāng)i=3 也就是到了第三個祖先 結(jié)果是body
當(dāng)i=4 也就是到了第四個祖先 結(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>無標(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+"個祖先是,("+$(this).attr("id")+")");
});
});
});
</script>
</head>
<body>
<input type="button" value="點擊" 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遍歷元素的父輩和祖先的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery3.0中的buildFragment私有函數(shù)詳解
在 jQuery3.0中,buildFragment 是一個私有函數(shù),用來構(gòu)建一個包含子節(jié)點 fragment 對象。下文給大家介紹jQuery3.0中的buildFragment私有函數(shù)詳解,對jquery3.0 buildfragment相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-08-08
jQuery 自動增長的文本輸入框?qū)崿F(xiàn)代碼
文本輸入框內(nèi)的字?jǐn)?shù)不能確定,而input type="text"的size是固定的,當(dāng)字?jǐn)?shù)超過size時(默認(rèn)是20),先輸入的內(nèi)容就會從文本框的左端隱藏起來,不便于輸入。2010-04-04

