JQuery 學習筆記 選取器之二_jquery

來源:互聯網
上載者:User

複製代碼 代碼如下:

<!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>
<script src="js/jquery-1.3.2.js" ></script>
<script type="text/javascript"><!--
$(function(){
$("#aDescendant").click(function(){
$("#Result").html("");
$("#div1 ul").each(function(){
$("#Result").html($("#Result").html() + $(this).html() + "," );
})
})
$("#aChild").click(function(){
$("#Result").html("");
$("#div1 > li").each(function(){
$("#Result").html($("#Result").html() + $(this).attr("id") + "," );
})
})
$("#aNext").click(function(){
$("#Result").html("");
$("label + input").each(function(){
$("#Result").html($("#Result").html() + $(this).attr("value") + ",");
})
})
$("#aSibling").click(function(){
$("#Result").html("");
$("#input1 ~ label").each(function(){
$("#Result").html($("#Result").html() + $(this).html() + ",");
})
})
})
// --></script>
</head>

<body>
<div id="div1">
<li id="l1">
<ul>1</ul>
<ul>2</ul>
<ul>3</ul>
<ul>4</ul>
</li>
<li id="l2">
<ul>一</ul>
<ul>二</ul>
<ul>三</ul>
<ul>四</ul>
</li>
<label>Label1</label>
<input id="input1" value="input1" />
<input />
<label>Label2</label>
<label>Label4</label>
</div>
<div>
<label>Label3</label>
<div style="border:1px solid #000" ></div>
<input id="input2" value="input2"/>
</div>
<label>Label4</label><br />
<a href="#" id="aDescendant">顯示DIV的後序結點ID</a>
<a href="#" id="aChild">顯示DIV的子LI結點</a>
<a href="#" id="aNext">顯示位於Label下一個input元素的value值</a>
<a href="#" id="aSibling">顯示於input1元素同級的label元素內容</a>
<br />
Result:
<br />
<div id="Result">
</div>
</body>
</html>

先對上面代碼中的課外知識點說明下
1.element.attr("attributeName")
描述:此方法使用者擷取某element元素的某個屬性值,如例子中
$("#div1 > li").each(function(){
$("#Result").html($("#Result").html() + $(this).attr("id") + "," );
})
功能就是擷取當前遍曆到的element對象的id值;
好了,現在開始來介紹本章的內容吧。本章主要講的就是JQuery裡如果去選擇某個結點的子結點、兄弟結點等,不浪費時間啦,哈,現在進入正題
1.$("Selector descendant")
描述:此方法主要用於擷取“Selector”選取器(註:此選取器為上一章講的幾個的任意一種)選擇的Element對象或集合的子孫結點,就像例子中
$("#aDescendant").click(function(){
$("#Result").html("");
$("#div1 ul").each(function(){
$("#Result").html($("#Result").html() + $(this).html() + "," );
})
})
功能就是擷取id為div1元素的子孫節點裡"ul"節點的HTMl內容,若“Selector”選取器返回的是個集合,則擷取的子孫結點就是這個集合裡,每個element匹配的子孫節點的集合
傳回值:Array(Element);
2.$("Selector>child")
描述:此方法與上一個方法類似,主要區別就是上個方法能擷取所有的子孫節點,而這方法只能擷取奇直屬的子節點(多個“>”號就代表是直屬的嘛^^),在此就不多說了,哈,其它都跟上一個一樣
傳回值:Array(Element);
3.$("Selector + next")
描述:用於擷取所有位於Selector後面的next元素,如例子中
$("#aNext").click(function(){
$("#Result").html("");
$("label + input").each(function(){
$("#Result").html($("#Result").html() + $(this).attr("value") + ",");
})
})
擷取所有位於label元素後面的input元素,在例子中,只有input1位於Label1下個節點,input2與Label3兩者隔了個div節點,所以不配合。
傳回值:Array(Element);
4.$("Selector ~ Sibling")
描述:與上一個方法類似,主要區別是此方法匹配的是位於Selector之後的所有同級的Sibling結點,無論中間是否有隔其它結點,在這也不明說啦,呵
傳回值:Array(Element);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.