html代码 层次选择器,juqery 学习之三 选择器 层级 基本

[

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 代码:

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

结果:

[ ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值