栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

结果:

[ ]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/114958.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号