DIV CSS 佈局教程網

3.2 簡單偽類選擇器:最常用的一組
編輯:jQuery基礎知識     

jQuery 提供的偽類選擇器很多,其中使用頻率最高的一批,我們稱為「簡單偽類選擇器」。

jQuery 簡單偽類選擇器
偽類選擇器說明
:not(selector)選擇除了某個選擇器之外的所有元素
:first 或 :first()選擇某元素的第 1 個元素(非子元素)
:last 或 :last()選擇某元素的最後 1 個元素(非子元素)
:odd選擇索引值為奇數的元素
:even選擇索引值為偶數的元素
:eq(index)選擇指定索引值的元素,index 為整數且從 0 開始
:lt(index)選擇索引值小於 index 的所有元素
:gt(index)選擇索引值大於 index 的所有元素
:header選擇 h1~h6 標題元素
:animated選擇所有正在執行動畫的元素
:root選擇頁面的根元素
:target選擇當前活動的目標元素(錨點)

舉例::not(selector)

 <!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
  *{padding:0;margin:0}
  ul{list-style-type:none;}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
  $(function () {
    $("li:not(#myLi)").css("color", "red");
  })
</script>
</head>
<body>
  <ul>
    <li>第1個選項</li>
    <li>第2個選項</li>
    <li id="myLi">第3個選項</li>
    <li>第4個選項</li>
    <li>第5個選項</li>
    <li>第6個選項</li>
  </ul>
</body>
</html>

分析:

$("li:not(#myLi)") 選出除了 id 為 myLi 之外的所有 li,這是「排除法」最直覺的寫法。

舉例::first、:last

 <!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
  *{padding:0;margin:0}
  ul{list-style-type:none;}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
  $(function () {
    $("li:first,li:last").css("color", "red");
  })
</script>
</head>
<body>
  <ul>
    <li>第1個選項</li>
    <li>第2個選項</li>
    <li>第3個選項</li>
    <li>第4個選項</li>
    <li>第5個選項</li>
    <li>第6個選項</li>
  </ul>
</body>
</html>

分析:

$("li:first,li:last") 是群組寫法,同時選中第一個與最後一個 li。

舉例::odd、:even、:eq()、:lt()、:gt()

 <!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
  *{padding:0;margin:0}
  ul{list-style-type:none;}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
  $(function () {
    $("li:odd").css("color", "red");
  })
</script>
</head>
<body>
  <ul>
    <li>第1個選項</li>
    <li>第2個選項</li>
    <li>第3個選項</li>
    <li>第4個選項</li>
    <li>第5個選項</li>
    <li>第6個選項</li>
  </ul>
</body>
</html>

分析:

$("li:odd") 選出索引值為奇數的 li。關鍵細節:索引是從 0 開始算,所以「第 1 個選項」的索引是 0,「第 2 個選項」是 1,依此類推。實際渲染時被上色的是第 2、4、6 項,很多人第一次看到都會愣一下。

你可以把範例中的 :odd 換成 :even、:eq(index)、:lt(index)、:gt(index) 逐一測試,效果會更好懂。

舉例::header

 <!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
  *{padding:0;margin:0}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
  $(function () {
    $(":header").css("color", "red");
  })
</script>
</head>
<body>
  <h1>這是一級標題</h1>
  <h2>這是二級標題</h2>
  <h3>這是三級標題</h3>
  <h4>這是四級標題</h4>
  <h5>這是五級標題</h5>
  <h6>這是六級標題</h6>
</body>
</html>

分析:

至於 :animated,它會選出正在執行動畫的元素,這要等我們學到 jQuery 動畫章節才有意義,這裡先不舉例,免得看了反而糊塗。:root 與 :target 在 jQuery 中幾乎用不到,有興趣可以對照 CSS3 的說明閱讀。

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