DIV CSS 佈局教程網

jQuery 學習筆記:選擇器之五(屬性過濾)
編輯:JQuery特效代碼     

本篇說明 jQuery 的屬性過濾選擇器,涵蓋屬性存在、相等、不等、開頭、結尾與包含等多種篩選方式。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery-1.3.2.js"></script>
<script type="text/javascript"><!--
$(function(){
$("#aFirst").click(function(){ $("div[id]").each(function(){ $(this).css("background","#773300"); }) })
$("#aSecond").click(function(){ $("div[id = div2]").each(function(){ $(this).css("background","#8833ff"); }) })
$("#aThird").click(function(){ $("div[id != div2]").each(function(){ $(this).css("background","#87f289"); }) })
$("#aFourthly").click(function(){ $("div[name ^= DIV]").each(function(){ $(this).css("background","#140586"); }) })
$("#aFifthly").click(function(){ $("div[name $= ly]").each(function(){ $(this).css("background","#930584"); }) })
$("#aSixth").click(function(){ $("div[name *= th]").each(function(){ $(this).css("background","#099483"); }) })
$("#aSeventh").click(function(){ $("div[id][name !=Fifthly][name *= i]").each(function(){ $(this).css("background","#938607"); }) })
})
// --></script>
<title>無標題文檔</title>
</head>
<body>
<a href="#" onclick="javascript:location.reload();">重置</a>
<div id="div1" name ="DIV_First">div1</div>
<div id="div2" name ="DIV_Second">div2</div>
<div id="div3" name = "DIV_Third">div3</div>
<div id="div4" name = "DIV_Fourthly">div4</div>
<div id="div5" name="Fifthly">div5</div>
<a href="#" id="aFirst">設置頁面所有 DIV 元素的背景顏色</a>|
<a href="#" id="aSecond">設置第2個 DIV 的背景顏色</a>|
<a href="#" id="aThird">設置除第2個 DIV 以外 DIV 的背景顏色</a>|
<a href="#" id="aFourthly">設置 name 屬性值以 DIV 開頭的元素</a>|
<a href="#" id="aFifthly">設置 name 屬性值以 ly 結尾的元素</a>|
<a href="#" id="aSixth">設置 name 屬性值包含 th 的元素</a>|
<a href="#" id="aSeventh">綜合應用</a>
</body>
</html>

選擇器說明

1. $("selector [Attribute]")(以下簡寫為 [Attribute])
取得 selector 所選元素集合中,擁有該屬性的元素。
返回值:Array(Element)。

2. [attribute=value]
取得屬性值等於 value 的元素集合。
返回值:Array(Element)。

3. [attribute!=value]
取得屬性值不等於 value 的元素集合。
返回值:Array(Element)。

4. [attribute^=value]
取得屬性值以 value 開頭的元素集合,概念近似正則。
返回值:Array(Element)。

5. [attribute$=value]
取得屬性值以 value 結尾的元素集合。
返回值:Array(Element)。

6. [attribute*=value]
取得屬性值包含 value 的元素集合。
返回值:Array(Element)。

7. [selector1][selector2][selectorN]
為前六種的綜合寫法。例如 $("div[id][name !=Fifthly][name *= i]") 會選取所有 div 中,擁有 id 屬性、name 屬性不等於 Fifthly、且 name 屬性包含字元 i 的元素集合。善用這種組合能大幅增加選擇的彈性。
返回值:Array(Element)。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved