經過整理,匯總最完整的 jQuery 選擇器與過濾器用法。所有代碼均經測試可用。文中包含 HTML 測試結構、DOM 與 jQuery 包裝集轉換、核心方法、各類選擇器分類示例。
<form id="form1" class="form1">
<table id="table1" class="table1">
<tr id="tr1" class="tr1">
<td id="td11" class="td11"><input type="text" id="text11" class="text11" value="text11"/><input type="button" id="button11" class="button11" value="button11"/></td>
<td id="td12" class="td12"><input type="text" id="text12" class="text12" value="text12"/><input type="hidden" id="hidden12" class="hidden12" value="hidden12"/></td>
<td id="td13" class="td13"><input type="text" id="text13" class="text13" value="text13"/></td>
</tr>
<tr id="tr2" class="tr2">
<td id="td21" class="td21"><input type="password" id="password21" class="password21" value="password21"/><input type="radio" id="radio21" class="radio21" value="radio21" checked/><input type="checkbox" id="checkbox21" class="checkbox21" value="checkbox21" checked/></td>
<td id="td22" class="td22"><input type="submit" id="submit22" class="submit22" value="submit22"/><input type="image" id="image22" class="image22" value="image22"/><input type="reset" id="reset22" class="reset22" value="reset22"/></td>
<td id="td23" class="td23"><input type="file" id="file23" class="file23" value="file23"/></td>
</tr>
<tr id="tr3" class="tr3">
<td id="td31" class="td31"><select id="select31"><option value="select311">option1</option><option value="select312" selected>option2</option><option value="select313">option3</option></select></td>
<td id="td32" class="td32"></td><td id="td33" class="td33"></td>
</tr>
</table>
</form>
<h1>你好世界!</h1>
$(domElement) 或 $(domCollection)$jq[0] 或 $jq.get(0)this 在事件處理函式或 .each() 回調中為原生 DOM 元素。jQuery(html, [ownerDocument]):依 HTML 字串動態建立 DOM 元素。jQuery(elements):將 DOM 元素封裝為 jQuery 包裝集。jQuery(callback):$(document).ready() 簡寫。jQuery(selector, [context]):在指定範圍內查找符合條件的包裝集。$("input") // 標籤名
$("#text11") // ID
$(".text11") // Class
$("#text11, .text12") // 多條件並集
$("*") // 所有元素
$("tr #text11") // 後代選擇器 (空格)
$("td > input") // 子元素選擇器 (>)
$("#text11 + .button11") // 相鄰兄弟 (+)
$("#text11 ~ .button11") // 一般兄弟 (~)
$("input:first") // 第一個
$("input:last") // 最後一個
$("input:not(:checked)") // 否定
$("input:even") // 索引為偶數 (0-based)
$("input:odd") // 索引為奇數
$("input:eq(2)") // 索引等於 2
$("input:gt(2)") // 索引大於 2
$("input:lt(2)") // 索引小於 2
$(":header") // h1~h6
$(":animated") // 正在動畫中
$("td:contains('text11')") // 含指定文字
$("td:empty") // 空元素 (無子節點、無文字)
$("td:parent") // 非空元素
$("td:has(input)") // 包含指定選擇器的後代
$("input:hidden") // type=hidden 或 display:none 等
$("input:visible") // 可見元素
$("[name]") // 擁有 name 屬性
$("[name='text11']") // 屬性等於
$("[name!='text11']") // 屬性不等於
$("[name^='text']") // 屬性以...開頭
$("[name$='11']") // 屬性以...結尾
$("[name*='ext']") // 屬性包含...
$("[name][id]") // 同時擁有多個屬性
$("tr:nth-child(2)") // 第 2 個子元素 (1-based)
$("tr:nth-child(even)") // 偶數子元素
$("tr:nth-child(2n+1)") // 公式
$("tr:first-child") // 第一個子元素
$("tr:last-child") // 最後一個子元素
$("tr:only-child") // 唯一子元素
$(":input") // 所有 input, textarea, select, button
$(":text") // type=text
$(":password") // type=password
$(":radio") // type=radio
$(":checkbox") // type=checkbox
$(":submit") // type=submit
$(":image") // type=image
$(":reset") // type=reset
$(":file") // type=file
$(":button") // type=button 或 button 元素
$(":enabled") // 可用表單元素
$(":disabled") // 禁用表單元素
$(":checked") // 被選中的 radio/checkbox
$(":selected") // 被選中的 option
熟練掌握上述選擇器與過濾器,能極大提升 DOM 操作效率。建議收藏備查,實戰中靈活組合使用。