JQuery遍曆元素的父輩和祖先的方法,jquery父輩祖先

來源:互聯網
上載者:User

JQuery遍曆元素的父輩和祖先的方法,jquery父輩祖先

JQuery遍曆

首先我們要知道 什麼是 父親,兒子,後代,同胞,祖先

•<div> 元素是 <ul> 的父元素,同時是其中所有內容的祖先。

•<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>無標題文檔</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){})

對其中的每個元素進行遍曆

其中i是索引,e是當前對象,相當於$(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>無標題文檔</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>

那你就會問了 第三個和第四個祖先是什麼鬼 讓我們在網頁中按F12調試一下 看看結果

當i=3 也就是到了第三個祖先 結果是body

 

當i=4 也就是到了第四個祖先 結果是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>無標題文檔</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遍曆元素的父輩和祖先的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.