#id
根據給定的ID匹配一個元素。
Matches a single element with the given id attribute.
返回值
Element
參數
id (String) : 用于搜索的,通過元素的 id 屬性中給定的值
示例
查找 ID 為"myDiv"的元素。
HTML 代碼:
id="notMe"
id="myDiv"
jQuery 代碼:
$("#myDiv");
結果:
[ id="myDiv" ]
---------------------------------------------------------------------------------------
element
根據給定的元素名匹配所有元素
Matches all elements with the given name.
返回值
Array
參數
element (String) : 一個用于搜索的元素。指向 DOM 節點的標簽名。
示例
查找一個 DIV 元素。
HTML 代碼:
DIV1
DIV2
SPAN
jQuery 代碼:
$("div");
結果:
[ DIV1, DIV2 ] ---------------------------------------------------------------------------------------
.class
根據給定的類匹配元素。
Matches all elements with the given class.
返回值
Array
參數
class (String) : 一個用以搜索的類。一個元素可以有多個類,只要有一個符合就能被匹配到。
示例
查找所有類是 "myClass" 的元素.
HTML 代碼:
div class="notMe"
div class="myClass"
span class="myClass"
jQuery 代碼:
$(".myClass");
結果:
[ div class="myClass", span class="myClass" ]
---------------------------------------------------------------------------------------
*
匹配所有元素
多用于結合上下文來搜索。
Matches all elements.
Most useful when combined with a context to search in.
返回值
Array
示例
找到每一個元素
HTML 代碼:
DIV
SPAN
P
jQuery 代碼:
$("*")
結果:
[ DIV, SPAN,
P
]
---------------------------------------------------------------------------------------
selector1,selector2,selectorN
將每一個選擇器匹配到的元素合并后一起返回。
你可以指定任意多個選擇器,并將匹配到的元素合并到一個結果內。
Matches the combined results of all the specified selectors.
You can specify any number of selectors to combine into a single result.
返回值
Array
參數
selector1 (Selector) : 一個有效的選擇器
selector2 (Selector) : 另一個有效的選擇器
selectorN (Selector) : (可選) 任意多個有效選擇器
示例
找到匹配任意一個類的元素。
HTML 代碼:
div
p class="myClass"
span
p class="notMyClass"
jQuery 代碼:
$("div,span,p.myClass")
結果:
[ div,
p class="myClass"
, span ] ---------------------------------------------------------------------------------------
ancestor descendant
在給定的祖先元素下匹配所有的后代元素
Matches all descendant elements specified by descendant of elements specified by ancestor.
返回值
Array
參數
ancestor (Selector) : 任何有效選擇器
descendant (Selector) : 用以匹配元素的選擇器,并且它是第一個選擇器的后代元素
示例
找到表單中所有的 input 元素
HTML 代碼:
jQuery 代碼:
$("form input")
結果:
[
,
]
---------------------------------------------------------------------------------------
parent > child
在給定的父元素下匹配所有的子元素
Matches all child elements specified by child of elements specified by parent.
返回值
Array
參數
parent (Selector) : 任何有效選擇器
child (Selector) : 用以匹配元素的選擇器,并且它是第一個選擇器的子元素
示例
匹配表單中所有的子級input元素。
HTML 代碼:
jQuery 代碼:
$("form > input")
結果:
[
]
---------------------------------------------------------------------------------------
prev + next
匹配所有緊接在 prev 元素后的 next 元素
Matches all next elements specified by next that are next to elements specified by prev.
返回值
Array
參數
prev (Selector) : 任何有效選擇器
next (Selector) :一個有效選擇器并且緊接著第一個選擇器
示例
匹配所有跟在 label 后面的 input 元素
HTML 代碼:
jQuery 代碼:
$("label + input")
結果:
[
,
]
---------------------------------------------------------------------------------------
prev ~ siblings
匹配 prev 元素之后的所有 siblings 元素
Matches all sibling elements after the "prev" element that match the filtering "siblings" selector.
返回值
Array
參數
prev (Selector) : 任何有效選擇器
siblings (Selector) : 一個選擇器,并且它作為第一個選擇器的同輩
示例
找到所有與表單同輩的 input 元素
HTML 代碼:
jQuery 代碼:
$("form ~ input")
結果:
[
]
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com