DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
jQuery 選擇器練習大全
編輯:CSS詳解     

jQuery 的選擇器非常強大,熟悉之後操作 DOM 可說是得心應手。有些選擇器與 CSS 偽類長得很像卻行為不同,特別容易混淆,需要多練習才能分辨。以下用一個可直接執行的完整頁面,把所有常用選擇器跑過一遍。

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<title>Test</title>
<meta charset="UTF-8" />
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  initElementBgColor();
});

var initElementBgColor = function() {
  $("div.smallBox:odd").css("background", "#ccc");
  $("div.smallBox:even").css("background", "#ccc");
  $(".smallBox:first").css("background", "#ccc");
  $(".smallBox:last").css("background", "#ccc");
  $(".smallBox:nth-child(2)").css("background", "#ccc");
  $(".smallBox:nth-child(n+1)").css("background", "#ccc");
  $(".smallBox:nth-child(2n)").css("background", "#ccc");
  $(".smallBox:nth-child(2n+1)").css("background", "#ccc");
  $(".smallBox:nth-child(-3n+100)").css("background", "#ccc");
  $(".smallBox:gt(3)").css("background", "#ccc");
  $(".smallBox:lt(3)").css("background", "#ccc");
  $(".smallBox:eq(5)").css("background", "#ccc");
  $(".smallBox:not(:empty)").css("background", "#ccc");
  $(".smallBox:hidden").css("display", "block");
  $(".smallBox:not(:visible)").css("display", "block");
  $(".box .smallBox,:visible").css("display", "block");
  $(".box .smallBox p:contains(4)").css("background", "#ccc");
  $(".box :header").css("background", "#ccc");
}
</script>
<style type="text/css">
* {margin: 0;padding: 0;}
.box {border: 1px solid;}
.smallBox {
  width: 100px;
  height: 100px;
  border: 1px solid red;
  margin-left: 91px;
  float: left;
  font-size: 80px;
  line-height: 100px;
  text-align: center;
}
.clear { clear: both; height: 0px; overflow: hidden; }
.show {display: none;}
.able {visibility: hidden;}
</style>
</head>
<body>
<div class="box">
<div class="smallBox">1</div>
<div class="smallBox">2</div>
<div class="smallBox">3</div>
<div class="smallBox"><p>4</p></div>
<div class="smallBox">5</div>
<div class="smallBox">6</div>
<div class="smallBox">7</div>
<div class="smallBox show">8</div>
<div class="smallBox able">9</div>
<div class="clear"></div>
<div>
<h1>H1H1H1H1H1H1H1H1H1H1H1H1H1H1H1</h1>
<h2>H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2</h2>
</div>
</div>
</body>
</html>

選擇器分類速查

基本順序類

  • :odd / :even:奇數、偶數項。注意 jQuery 的索引從 0 開始,所以 :odd 選到的是實際上的第 1、3、5 個元素,這點與 CSS 的直覺相反,最容易搞混。
  • :first / :last:集合中的第一個與最後一個。
  • :eq(n):索引為 n 的元素。
  • :gt(n) / :lt(n):索引大於 n /小於 n 的元素。

nth-child 系列

  • :nth-child(2):父元素下的第 2 個子元素。這與 CSS 的 nth-child 行為一致,索引從 1 開始。
  • :nth-child(n+1):第 1 個之後的全部。
  • :nth-child(2n):偶數位。
  • :nth-child(2n+1):奇數位。
  • :nth-child(-3n+100):這是個有趣的式子,會選出第 1 到 100 個中符合 3 的倍數倒推的項目,實務上常用來做「倒數第 N 個」之類的變化。

內容與狀態類

  • :not(:empty):選出非空元素。
  • :contains(text):選出內容包含指定文字的元素,例如 p:contains(4) 會命中內容有「4」的段落。
  • :hidden:選出隱藏元素。注意 jQuery 判斷的標準比 CSS 寬鬆,display:none、visibility:hidden、寬高為 0 都算隱藏。因此範例中的 .show(display:none)與 .able(visibility:hidden)都會被 :hidden 命中。
  • :visible:反之,選出可見元素。:not(:visible) 等同於 :hidden。
  • :header:選出所有標題元素(h1 到 h6)。

幾個容易踩的雷

:odd 與 :nth-child(odd) 不同。前者依 jQuery 集合的 0 起算索引,後者依 DOM 中的 1 起算位置,兩者結果剛好相反。這是新手最常出錯的地方。

:hidden 的判定範圍比想像中大。只要元素不佔據任何可見空間就會被選中,包含 width:0、height:0 的情況。要精準控制建議改用 class。

效能考量。jQuery 的 :contains、:header 等自訂偽類無法交給瀏覽器的 querySelectorAll 原生處理,必須在 JavaScript 層逐一過濾。在大量元素上使用會明顯變慢,優先改用 class 或屬性選擇器會快得多。

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