您好,欢迎来到财客网。
搜索
您的当前位置:首页juqery学习之三选择器层级基本_jquery

juqery学习之三选择器层级基本_jquery

来源:财客网


#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")

结果:

[ ] if ($ != jQuery) { $ = jQuery.noConflict(); }

Copyright © 2019- ckjp.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务