匹配找到的第一個元素
--------------------------------------------------------------------------------
Matches the first selected element.
返回值
Element
示例
查找表格的第一行
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:first")
結果:
[ <tr><td>Header 1</td></tr> ]
---------------------------------------------------------------------------------------
:last匹配找到的最后一個元素
--------------------------------------------------------------------------------
Matches the last selected element.
返回值
Element
示例
查找表格的最后一行
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:last")
結果:
[ <tr><td>Value 2</td></tr> ]
---------------------------------------------------------------------------------------
:not(selector)去除所有與給定選擇器匹配的元素
--------------------------------------------------------------------------------
Removes all elements matching the given selector.
返回值
Array<Element>
參數
selector (Selector) : 用于篩選的選擇器
示例
查找所有未選中的 input 元素
HTML 代碼:
<input name="apple" />
<input name="flower" checked="checked" />
jQuery 代碼:
$("input:not(:checked)")
結果:
[ <input name="apple" /> ]
---------------------------------------------------------------------------------------
:even匹配所有索引值為偶數的元素,從 0 開始計數
--------------------------------------------------------------------------------
Matches even elements, zero-indexed.
返回值
Array<Element>
示例
查找表格的1、3、5...行(即索引值0、2、4...)
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:even")
結果:
[ <tr><td>Header 1</td></tr>, <tr><td>Value 2</td></tr> ]
---------------------------------------------------------------------------------------
:odd匹配所有索引值為奇數的元素,從 0 開始計數
--------------------------------------------------------------------------------
Matches odd elements, zero-indexed.
返回值
Array<Element>
示例
查找表格的2、4、6行(即索引值1、3、5...)
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:odd")
結果:
[ <tr><td>Value 1</td></tr> ]
---------------------------------------------------------------------------------------
:eq(index)匹配一個給定索引值的元素
--------------------------------------------------------------------------------
Matches a single element by its index.
返回值
Element
參數
index (Number) : 從 0 開始計數
示例
查找第二行
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:eq(1)")
結果:
[ <tr><td>Value 1</td></tr> ]
---------------------------------------------------------------------------------------
:gt(index)匹配所有大于給定索引值的元素
--------------------------------------------------------------------------------
Matches all elements with an index above the given one.
返回值
Array<Element>
參數
index (Number) : 從 0 開始計數
示例
查找第二第三行,即索引值是1和2,也就是比0大
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:gt(0)")
結果:
[ <tr><td>Value 1</td></tr>, <tr><td>Value 2</td></tr> ]
---------------------------------------------------------------------------------------
:lt(index)匹配所有小于給定索引值的元素
--------------------------------------------------------------------------------
Matches all elements with an index below the given one.
返回值
Array<Element>
參數
index (Number) : 從 0 開始計數
示例
查找第一第二行,即索引值是0和1,也就是比2小
HTML 代碼:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:lt(2)")
結果:
[ <tr><td>Header 1</td></tr>, <tr><td>Value 1</td></tr> ]
---------------------------------------------------------------------------------------
:header匹配如 h1, h2, h3之類的標題元素
--------------------------------------------------------------------------------
Matches all elements that are headers, like h1, h2, h3 and so on.
返回值
Array<Element>
示例
給頁面內所有標題加上背景色
HTML 代碼:
<h1>Header 1</h1>
<p>Contents 1</p>
<h2>Header 2</h2>
<p>Contents 2</p>
jQuery 代碼:
$(":header").css("background", "#EEE");
結果:
[ <h1 style="background:#EEE;">Header 1</h1>, <h2 style="background:#EEE;">Header 2</h2> ]
---------------------------------------------------------------------------------------
:animated匹配所有沒有在執行動畫效果中的元素
--------------------------------------------------------------------------------
Matches all elements that are currently being animated.
返回值
Array<Element>
示例
只有對不在執行動畫效果的元素執行一個動畫特效
HTML 代碼:
<button id="run">Run</button><div></div>
jQuery 代碼:
$("#run").click(function(){
$("div:not(:animated)").animate({ left: "+20" }, 1000);
});
---------------------------------------------------------------------------------------
:contains(text)匹配包含給定文本的元素
--------------------------------------------------------------------------------
Matches elements which contain the given text.
返回值
Array<Element>
參數
text (String) : 一個用以查找的字符串
示例
查找所有包含 "John" 的 div 元素
HTML 代碼:
<div>John Resig</div>
<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn
jQuery 代碼:
$("div:contains('John')")
結果:
[ <div>John Resig</div>, <div>Malcom John Sinclair</div> ]
---------------------------------------------------------------------------------------
:empty匹配所有不包含子元素或者文本的空元素
--------------------------------------------------------------------------------
Matches all elements that are empty, be it elements or text.
返回值
Array<Element>
示例
查找所有不包含子元素或者文本的空元素
HTML 代碼:
<table>
<tr><td>Value 1</td><td></td></tr>
<tr><td>Value 2</td><td></td></tr>
</table>
jQuery 代碼:
$("td:empty")
結果:
[ <td></td>, <td></td> ]
---------------------------------------------------------------------------------------
:has(selector)匹配含有選擇器所匹配的元素的元素
--------------------------------------------------------------------------------
Matches elements which contain at least one element that matches the specified selector.
返回值
Array<Element>
參數
selector (Selector) : 一個用于篩選的選擇器
示例
給所有包含 p 元素的 div 元素添加一個 text 類
HTML 代碼:
<div><p>Hello</p></div>
<div>Hello again!</div>
jQuery 代碼:
$("div:has(p)").addClass("test");
結果:
[ <div class="test"><p>Hello</p></div> ]
---------------------------------------------------------------------------------------
:parent匹配含有子元素或者文本的元素
--------------------------------------------------------------------------------
Matches all elements that are parents - they have child elements, including text.
返回值
Array<Element>
示例
查找所有含有子元素或者文本的 td 元素
HTML 代碼:
<table>
<tr><td>Value 1</td><td></td></tr>
<tr><td>Value 2</td><td></td></tr>
</table>
jQuery 代碼:
$("td:parent")
新聞熱點
疑難解答