:hidden
匹配所有的不可見元素,input 元素的 type 屬性為 "hidden" 的話也會被匹配到
Matches all elements that are hidden, or input elements of type "hidden".
返回值
Array<Element>
示例
查找所有不可見的 tr 元素
HTML 代碼:
<table>
<tr style="display:none"><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:hidden")
結果:
[ <tr style="display:none"><td>Value 1</td></tr> ]
---------------------------------------------------------------------------------------
:visible
匹配所有的可見元素
Matches all elements that are visible.
返回值
Array<Element>
示例
查找所有可見的 tr 元素
HTML 代碼:
<table>
<tr style="display:none"><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代碼:
$("tr:visible")
結果:
[ <tr><td>Value 2</td></tr> ]
---------------------------------------------------------------------------------------
[attribute]
匹配包含給定屬性的元素
Matches elements that have the specified attribute.
返回值
Array<Element>
參數
attribute (String) : 屬性名
示例
查找所有含有 id 屬性的 div 元素
HTML 代碼:
<div>
<p>Hello!</p>
</div>
<div id="test2"></div>
jQuery 代碼:
$("div[id]")
結果:
[ <div id="test2"></div> ]
---------------------------------------------------------------------------------------
[attribute=value]
匹配給定的屬性是某個特定值的元素
Matches elements that have the specified attribute with a certain value.
返回值
Array<Element>
參數
attribute (String) : 屬性名
value (String) : 屬性值。引號在大多數情況下是可選的。但在遇到諸如屬性值包含"]"時,用以避免沖突。
示例
查找所有 name 屬性是 newsletter 的 input 元素
HTML 代碼:
'<input type="checkbox" name="newsletter" value="Hot Fuzz" />
<input type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />
jQuery 代碼:
$("input[name='newsletter']").attr("checked", true);
結果:
[ <input type="checkbox" name="newsletter" value="Hot Fuzz" checked="true" />, <input type="checkbox" name="newsletter" value="Cold Fusion" checked="true" /> ]
---------------------------------------------------------------------------------------
[attribute!=value]
匹配給定的屬性是不包含某個特定值的元素
Matches elements that don't have the specified attribute with a certain value.
返回值
Array<Element>
參數
attribute (String) : 屬性名
value (String) : 屬性值。引號在大多數情況下是可選的。但在遇到諸如屬性值包含"]"時,用以避免沖突。
示例
查找所有 name 屬性不是 newsletter 的 input 元素
HTML 代碼:
'<input type="checkbox" name="newsletter" value="Hot Fuzz" />
<input type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />
jQuery 代碼:
$("input[name!='newsletter']").attr("checked", true);
結果:
[ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ]
---------------------------------------------------------------------------------------
[attribute^=value]
匹配給定的屬性是以某些值開始的元素
Matches elements that have the specified attribute and it starts with a certain value.
返回值
Array<Element>
參數
attribute (String) : 屬性名
value ( String) : 屬性值。引號在大多數情況下是可選的。但在遇到諸如屬性值包含"]"時,用以避免沖突。
示例
查找所有 name 以 'news' 開始的 input 元素
HTML 代碼:
<input name="newsletter" />
<input name="milkman" />
<input name="newsboy" />
jQuery 代碼:
$("input[name^='news']")
結果:
[ <input name="newsletter" />, <input name="newsboy" /> ]
---------------------------------------------------------------------------------------
[attribute$=value]
匹配給定的屬性是以某些值結尾的元素
Matches elements that have the specified attribute and it ends with a certain value.
返回值
Array<Element>
參數
attribute (String) : 屬性名
value (String) : 屬性值。引號在大多數情況下是可選的。但在遇到諸如屬性值包含"]"時,用以避免沖突。
示例
查找所有 name 以 'letter' 結尾的 input 元素
HTML 代碼:
<input name="newsletter" />
<input name="milkman" />
<input name="jobletter" />
jQuery 代碼:
$("input[name$='letter']")
結果:
[ <input name="newsletter" />, <input name="jobletter" /> ]
---------------------------------------------------------------------------------------
[attribute*=value]
匹配給定的屬性是以包含某些值的元素
Matches elements that have the specified attribute and it contains a certain value.
返回值
Array<Element>
參數
attribute (String) : 屬性名
value (String) : 屬性值。引號在大多數情況下是可選的。但在遇到諸如屬性值包含"]"時,用以避免沖突。
示例
查找所有 name 包含 'man' 的 input 元素
HTML 代碼:
<input name="man-news" />
<input name="milkman" />
<input name="letterman2" />
<input name="newmilk" />
jQuery 代碼:
$("input[name*='man']")
結果:
[ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ]
---------------------------------------------------------------------------------------
[selector1][selector2][selectorN]
復合屬性選擇器,需要同時滿足多個條件時使用。
Matches elements that have the specified attribute and it contains a certain value.
返回值
Array<Element>
參數
selector1 (Selector) : 屬性選擇器
selector2 (Selector) : 另一個屬性選擇器,用以進一步縮小范圍
selectorN (Selector) : 任意多個屬性選擇器
示例
找到所有含有 id 屬性,并且它的 name 屬性是以 man 結尾的
HTML 代碼:
<input id="man-news" name="man-news" />
<input name="milkman" />
<input id="letterman" name="new-letterman" />
<input name="newmilk" />
jQuery 代碼:
$("input[id][name$='man']")
結果:
[ <input id="letterman" name="new-letterman" /> ]