操作jQuery集合搜索父元素

搜索父元素

1.1parents()方法

parents()方法用於獲取u當前匹配元素集合中的每個元素的祖先元素,根據需要還可以使用一個選擇器進行篩選parents([selector])

其中selector參數是可選的,表示用來篩選的表達式,即查找祖先元素可以滿足的篩選條件。如未提供該參數,則返回每個匹配元素所有的祖先元素,如以下HTML代碼:

<!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=gb2312" />
<title>無標題文檔</title>
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
			   $('p').parents().css("border","1px solid #F00");
			   });
</script>
</head>
<body>
	<div style="width:300px;height:150px;">
    	<div style="width:200px;height:100px;margin-top:10px;margin-left:30px;">
        	<p>給祖先元素添加邊框樣式</p>
        </div>
    </div>
</body>
</html>

p的祖先元素爲倆個div和body元素

1.2closest()方法

closest()方法是jQuery1.3版本中新增的,該方法從元素本身開始,逐級向上級元素匹配,並返回最先匹配的元素,語法格式closest(selector,[context])

其中參數selector是一個包含選擇器表達式的字符串或字符串數組,用於指定元素的匹配條件,參數context是可選的,指定一個DOM元素,可以在其中找到匹配元素

該方法首先檢查當前元素是否匹配,如果匹配則直接返回元素本身,如果不匹配則向上查找父元素一層一層往上,知道找到匹配選擇器的元素,如果什麼也沒找到則返回一個空的jQuery對象。

例如HTML代碼

<!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=gb2312" />
<title>無標題文檔</title>
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
			   $('a').closest("div").css("border","2px solid #F00");
			   /*執行該代碼以後,將改變id爲firstdiv的div元素的邊框樣式,因爲它是向上遍歷DOM樹遇到的
		第一個匹配元素
		*/
			   });
</script>
</head>
<body>
	<div id="maindiv">
    	<div id="firstdiv">
        	<a>CLOSEST()方法</a>
        </div>
    </div>
</body>
</html>

1.3parent()方法

parent()方法用於獲取當前匹配元素集合中每個元素的父元素,根據需要還可以使用一個選擇器進行篩選,語法格式:parent([selector])

其中參數selector是可選的表示用來篩選的表達式

parent()與parents方法累世,只不過前者只遍歷了DOM樹的一個層級,而後者可遍歷DOM樹的多個層級,如下HTML代碼

<!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=gb2312" />
<title>無標題文檔</title>
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
			  $("p").parent().css("border","1px solid blue");
			  //執行代碼將改變id=divtop的div邊框樣式
			   });
</script>
</head>
<body>
	<div style="widht:300px;height:150px;" id="div_main">
    	<div id="div_top" style="width:200px;height:100px;margin-top:10px;margin-left:30px;">
         	<p>給父元素添加邊框樣式</p>
        </div>
    </div>
</body>
</html>

1.4parentsUntil()方法

parentsUntil()方法用於獲取當前匹配元素集合中每個元素的祖先元素,直到給定選擇器匹配的元素,語法格式:parentsUntil([selector])

其中,參數selector是可選的,其值是一個包含選擇器的表達式字符串,用來匹配元素的祖先,HTML代碼如下:

<!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=gb2312" />
<title>無標題文檔</title>
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
			 $("#sonullisecond").parentsUntil("secondli").css("background","red");
			 //找到#sonullisecond到祖先元素secondli之間的元素,併爲這些元素添加背景
			   });
</script>
</head>
<body>
	<ul id="ul_first">
		<li id="firstli">firstli</li>
        <li id="secondli">
        	<ul id="sonul">
            	<li id="sonullifirst">sonullifirst</li>
                <li id="sonullisecond">sonullisecond</li>
            </ul>
        </li>
        <li id="thirdli">thirdli</li>    
    </ul>
</body>
</html>

1.5offsetParent()方法

offsetParent()方法用於搜索第一個匹配元素的已定位的父元素,僅對可見元素有效,語法格式:offsetParent()

該方法查找第一個匹配元素的已定位元素,並返回由該元素包裝成的jQuery對象。

下面舉例說明如何在文檔中獲取指定元素的祖先元素和父元素

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章