[
---------------------------------------------------------------------------------------
element
根据给定的元素名匹配所有元素
Matches all elements with the given name.
返回值
Array
参数
element(String) : 一个用于搜索的元素。指向 DOM 节点的标签名。
示例
查找一个 DIV 元素。
HTML 代码:
SPAN
jQuery 代码:
$("div");
结果:
[
.class
根据给定的类匹配元素。
Matches all elements with the given class.
返回值
Array
参数
class(String) : 一个用以搜索的类。一个元素可以有多个类,只要有一个符合就能被匹配到。
示例
查找所有类是 "myClass" 的元素.
HTML 代码:
span class="myClass"
jQuery 代码:
$(".myClass");
结果:
[
---------------------------------------------------------------------------------------
*
匹配所有元素
多用于结合上下文来搜索。
Matches all elements.
Most useful when combined with a context to search in.
返回值
Array
示例
找到每一个元素
HTML 代码:
SPAN
P
jQuery 代码:
$("*")
结果:
[
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 代码:
p class="myClass"
span
p class="notMyClass"
jQuery 代码:
$("div,span,p.myClass")
结果:
[
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 代码:
Name:
Newsletter:
jQuery 代码:
$("form input")
结果:
[ , ]
---------------------------------------------------------------------------------------
parent > child
在给定的父元素下匹配所有的子元素
Matches all child elements specified by child of elements specified by parent.
返回值
Array
参数
parent(Selector) : 任何有效选择器
child(Selector) : 用以匹配元素的选择器,并且它是第一个选择器的子元素
示例
匹配表单中所有的子级input元素。
HTML 代码:
Name:
Newsletter:
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 代码:
Name:
Newsletter:
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 代码:
Name:
Newsletter:
jQuery 代码:
$("form ~ input")
结果:
[ ]