juqery 学习之四 筛选查找


Posted in Javascript onNovember 30, 2010

add(expr)

把与表达式匹配的元素添加到jQuery对象中。这个函数可以用于连接分别与两个表达式匹配的元素结果集。

Adds more elements, matched by the given expression, to the set of matched elements.

返回值

jQuery

参数

expr (String, DOMElement, Array<DOMElement>) : 用于匹配元素并添加的表达式字符串,或者用于动态生成的HTML代码,如果是一个字符串数组则返回多个元素

示例

添加一个新元素到一组匹配的元素中,并且这个新元素能匹配给定的表达式。

HTML 代码:

<p>Hello</p><span>Hello Again</span>

jQuery 代码:

$("p").add("span")

结果:

[ <p>Hello</p>, <span>Hello Again</span> ]

动态生成一个元素并添加至匹配的元素中

HTML 代码:

<p>Hello</p>

jQuery 代码:

$("p").add("<span>Again</span>")

结果:

[ <p>Hello</p>, <span>Hello Again</span> ]

为匹配的元素添加一个或者多个元素

HTML 代码:

<p>Hello</p><p><span id="a">Hello Again</span></p>

jQuery 代码:

$("p").add(document.getElementById("a"))

结果:

[ <p>Hello</p>, <p><span id="a">Hello Again</span></p>, <span id="a">Hello Again</span> ]

----------------------------------------------------------------------------------------------------------------

children([expr])

取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合。
可以通过可选的表达式来过滤所匹配的子元素。注意:parents()将查找所有祖辈元素,而children()之考虑子元素而不考虑所有后代元素。

Get a set of elements containing all of the unique children of each of the matched set of elements.
This set can be filtered with an optional expression that will cause only elements matching the selector to be collected. Also note: while parents() will look at all ancestors, children() will only consider immediate child elements.

返回值

jQuery

参数

expr (String) : (可选) 用以过滤子元素的表达式

示例

查找DIV中的每个子元素。

HTML 代码:

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

jQuery 代码:

$("div").children()

结果:

[ <span>Hello Again</span> ]

在每个div中查找 .selected 的类。

HTML 代码:

<div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>

jQuery 代码:

$("div").children(".selected")

结果:

[ <p class="selected">Hello Again</p> ]

----------------------------------------------------------------------------------------------------------------

contents()

查找匹配元素内部所有的子节点(包括文本节点)。如果元素是一个iframe,则查找文档内容

Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.

返回值

jQuery

示例

查找所有文本节点并加粗

HTML 代码:

<p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p>

jQuery 代码:

$("p").contents().not("[@nodeType=1]").wrap("<b/>");

结果:

<p><b>Hello</b> <a href="http://ejohn.org/">John</a>, <b>how are you doing?</b></p>

往一个空框架中加些内容

HTML 代码:

<iframe src="/index-blank.html" width="300" height="100"></iframe>

jQuery 代码:

$("iframe").contents().find("body")   .append("I'm in an iframe!");

----------------------------------------------------------------------------------------------------------------

find(expr)

搜索所有与指定表达式匹配的元素。这个函数是找出正在处理的元素的后代元素的好方法。
所有搜索都依靠jQuery表达式来完成。这个表达式可以使用CSS1-3的选择器语法来写。

Searches for all elements that match the specified expression. This method is a good way to find additional descendant elements with which to process.
All searching is done using a jQuery expression. The expression can be written using CSS 1-3 Selector syntax.

返回值

jQuery

参数

expr (String) :用于查找的表达式

示例

从所有的段落开始,进一步搜索下面的span元素。与$("p span")相同。

HTML 代码:

<p><span>Hello</span>, how are you?</p>

jQuery 代码:

$("p").find("span")

结果:

[ <span>Hello</span> ]

----------------------------------------------------------------------------------------------------------------

next([expr])

取得一个包含匹配的元素集合中每一个元素紧邻的后面同辈元素的元素集合。
这个函数只返回后面那个紧邻的同辈元素,而不是后面所有的同辈元素(可以使用nextAll)。可以用一个可选的表达式进行筛选。

Get a set of elements containing the unique next siblings of each of the matched set of elements.
It only returns the very next sibling for each element, not all next siblings (nor does it return the next closest sibling). You may provide an optional expression to filter the match.

返回值

jQuery

参数

expr (String) : (可选) 用于筛选的表达式

示例

找到每个段落的后面紧邻的同辈元素。

HTML 代码:

<p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>

jQuery 代码:

$("p").next()

结果:

[ <p>Hello Again</p>, <div><span>And Again</span></div> ]

找到每个段落的后面紧邻的同辈元素中类名为selected的元素。

HTML 代码:

<p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>

jQuery 代码:

$("p").next(".selected")

结果:

[ <p class="selected">Hello Again</p> ]

----------------------------------------------------------------------------------------------------------------

nextAll([expr])

查找当前元素之后的所有元素。
可以用表达式过滤

Find all sibling elements after the current element.
Use an optional expression to filter the matched set.

返回值

jQuery

参数

expr (String) : (可选)用来过滤的表达式

示例

给第一个div之后的所有元素加个类

HTML 代码:

<div></div><div></div><div></div><div></div>

jQuery 代码:

$("div:first").nextAll().addClass("after");

结果:

[ <div class="after"></div>, <div class="after"></div>, <div class="after"></div> ]

----------------------------------------------------------------------------------------------------------------

parent([expr])

取得一个包含着所有匹配元素的唯一父元素的元素集合。
你可以使用可选的表达式来筛选。

Get a set of elements containing the unique parents of the matched set of elements.
You may use an optional expression to filter the set of parent elements that will match.

返回值

jQuery

参数

expr (String) : (可选)用来筛选的表达式

示例

查找每个段落的父元素

HTML 代码:

<div><p>Hello</p><p>Hello</p></div>

jQuery 代码:

$("p").parent()

结果:

[ <div><p>Hello</p><p>Hello</p></div> </body> ]

查找段落的父元素中每个类名为selected的父元素。

HTML 代码:

<div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>

jQuery 代码:

$("p").parent(".selected")

结果:

[ <div class="selected"><p>Hello Again</p></div> ]

----------------------------------------------------------------------------------------------------------------

parents([expr])

取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素)。可以通过一个可选的表达式进行筛选。

Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element). The matched elements can be filtered with an optional expression.

返回值

jQuery

参数

expr (String) : (可选) 用于筛选祖先元素的表达式

示例

找到每个span元素的所有祖先元素。

HTML 代码:

<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>

jQuery 代码:

$("span").parents()

找到每个span的所有是p元素的祖先元素。

jQuery 代码:

$("span").parents("p")

----------------------------------------------------------------------------------------------------------------

prev([expr])

取得一个包含匹配的元素集合中每一个元素紧邻的前一个同辈元素的元素集合。
可以用一个可选的表达式进行筛选。只有紧邻的同辈元素会被匹配到,而不是前面所有的同辈元素。

Get a set of elements containing the unique previous siblings of each of the matched set of elements.
Use an optional expression to filter the matched set. Only the immediately previous sibling is returned, not all previous siblings.

返回值

jQuery

参数

expr (String) : (可选) 用于筛选前一个同辈元素的表达式

示例

找到每个段落紧邻的前一个同辈元素。

HTML 代码:

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

jQuery 代码:

$("p").prev()

结果:

[ <div><span>Hello Again</span></div> ]

找到每个段落紧邻的前一个同辈元素中类名为selected的元素。

HTML 代码:

<div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>

jQuery 代码:

$("p").prev(".selected")

结果:

[ <p class="selected">Hello Again</p> ]

----------------------------------------------------------------------------------------------------------------

prevAll([expr])

查找当前元素之前所有的同辈元素
可以用表达式过滤。

Find all sibling elements before the current element.
Use an optional expression to filter the matched set.

返回值

jQuery

参数

expr (String) : (可选) 用于过滤的表达式

示例

给最后一个之前的所有div加上一个类

HTML 代码:

<div></div><div></div><div></div><div></div>

jQuery 代码:

$("div:last").prevAll().addClass("before");

结果:

[ <div class="before"></div>, <div class="before"></div>, <div class="before"></div> ]

----------------------------------------------------------------------------------------------------------------

siblings([expr])

取得一个包含匹配的元素集合中每一个元素的所有唯一同辈元素的元素集合。可以用可选的表达式进行筛选。

Get a set of elements containing all of the unique siblings of each of the matched set of elements. Can be filtered with an optional expressions.

返回值

jQuery

参数

expr (String) : (可选) 用于筛选同辈元素的表达式

示例

找到每个div的所有同辈元素。

HTML 代码:

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

jQuery 代码:

$("div").siblings()

结果:

[ <p>Hello</p>, <p>And Again</p> ]

找到每个div的所有同辈元素中带有类名为selected的元素。

HTML 代码:

<div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>

jQuery 代码:

$("p").siblings(".selected")

结果:

[ <p class="selected">Hello Again</p> ]
------------------------------------------------------------------------------------------------------------------------

andSelf()

加入先前所选的加入当前元素中
对于筛选或查找后的元素,要加入先前所选元素时将会很有用。

Add the previous selection to the current selection.
Useful for traversing elements, and then adding something that was matched before the last traversion.

返回值

jQuery

示例

选取所有div以及内部的p,并加上border类

HTML 代码:

<div><p>First Paragraph</p><p>Second Paragraph</p></div>

jQuery 代码:

$("div").find("p").andSelf().addClass("border");

结果:

<div class="border"><p class="border">First Paragraph</p><p class="border">Second Paragraph</p></div>

------------------------------------------------------------------------------------------------------------------------

end()

回到最近的一个"破坏性"操作之前。即,将匹配的元素列表变为前一次的状态。
如果之前没有破坏性操作,则返回一个空集。所谓的"破坏性"就是指任何改变所匹配的jQuery元素的操作。这包括在 Traversing 中任何返回一个jQuery对象的函数--'add', 'andSelf', 'children', 'filter', 'find', 'map', 'next', 'nextAll', 'not', 'parent', 'parents', 'prev', 'prevAll', 'siblings' and 'slice'--再加上 Manipulation 中的 'clone'。

Revert the most recent 'destructive' operation, changing the set of matched elements to its previous state (right before the destructive operation).
If there was no destructive operation before, an empty set is returned. A 'destructive' operation is any operation that changes the set of matched jQuery elements, which means any Traversing function that returns a jQuery object - including 'add', 'andSelf', 'children', 'filter', 'find', 'map', 'next', 'nextAll', 'not', 'parent', 'parents', 'prev', 'prevAll', 'siblings' and slice - plus the 'clone' function (from Manipulation).

返回值

jQuery

示例

选取所有的p元素,查找并选取span子元素,然后再回过来选取p元素

HTML 代码:

<p><span>Hello</span>,how are you?</p>

jQuery 代码:

$("p").find("span").end()

结果:

[ <p><span>Hello</span> how are you?</p> ]
Javascript 相关文章推荐
Firefox中使用outerHTML的2种解决方法
Jun 07 Javascript
纯js实现div内图片自适应大小(已测试,兼容火狐)
Jun 16 Javascript
node.js中的fs.fchownSync方法使用说明
Dec 16 Javascript
使用jquery动态加载Js文件和Css文件
Oct 24 Javascript
Jquery 1.9.1源码分析系列(十二)之筛选操作
Dec 02 Javascript
AngularJS 最常用的功能汇总
Feb 17 Javascript
jQuery Easyui学习教程之实现datagrid在没有数据时显示相关提示内容
Jul 09 Javascript
详解Vue生命周期的示例
Mar 10 Javascript
基于vuejs实现一个todolist项目
Apr 11 Javascript
layui表格内容溢出的解决方法
Sep 06 Javascript
vue props对象validator自定义函数实例
Nov 13 Javascript
Vue实现可移动水平时间轴
Jun 29 Javascript
用XMLDOM和ADODB.Stream实现base64编码解码实现代码
Nov 28 #Javascript
xss文件页面内容读取(解决)
Nov 28 #Javascript
用js来解决ajax读取页面乱码
Nov 28 #Javascript
window.name代替cookie的实现代码
Nov 28 #Javascript
在一个js文件里远程调用jquery.js会在ie8下的一个奇怪问题
Nov 28 #Javascript
一个网马的tips实现分析
Nov 28 #Javascript
JQUBAR1.1 jQuery 柱状图插件发布
Nov 28 #Javascript
You might like
PHP EOT定界符的使用详解
2008/09/30 PHP
php中获得视频时间总长度的另一种方法
2011/09/15 PHP
win7下memCache的安装过程(具体操作步骤)
2013/06/28 PHP
详解Yii实现分页的两种方法
2017/01/14 PHP
javascript学习笔记(七) js函数介绍
2012/06/19 Javascript
SOSO地图JS画出标注和中心点以html形式运行
2013/08/09 Javascript
用JS做的简单的可折叠的两级树形菜单
2013/09/21 Javascript
node.js中的http.createClient方法使用说明
2014/12/15 Javascript
JavaScript实现图像模糊化的方法实例
2017/01/15 Javascript
jQuery实现按比例缩放图片的方法
2017/04/29 jQuery
JS实现微信里判断页面是否被分享成功的方法
2017/06/06 Javascript
vue中element 上传功能的实现思路
2018/07/06 Javascript
解决vue 界面在苹果手机上滑动点击事件等卡顿问题
2018/11/27 Javascript
js变量声明var使用与不使用的区别详解
2019/01/21 Javascript
js中Generator函数的深入讲解
2019/04/07 Javascript
JavaScript使用面向对象实现的拖拽功能详解
2019/06/12 Javascript
vue中如何实现后台管理系统的权限控制的方法步骤
2019/09/05 Javascript
[52:09]2014 DOTA2华西杯精英邀请赛 5 25 NewBee VS DK第二场
2014/05/26 DOTA
[01:52]2020年DOTA2 TI10夏季活动预告片
2020/07/15 DOTA
python网络爬虫之如何伪装逃过反爬虫程序的方法
2017/11/23 Python
遗传算法python版
2018/03/19 Python
Pyinstaller将py打包成exe的实例
2018/03/31 Python
python将回车作为输入内容的实例
2018/06/23 Python
python aiohttp的使用详解
2019/06/20 Python
Django后台admin的使用详解
2019/07/08 Python
解决Python import docx出错DLL load failed的问题
2020/02/13 Python
python接口自动化之ConfigParser配置文件的使用详解
2020/08/03 Python
Web前端绘制0.5像素的几种方法
2017/08/11 HTML / CSS
马来西亚太阳镜、眼镜和隐形眼镜网上商店:Focus Point
2018/12/13 全球购物
Flesh Beauty官网:露华浓集团旗下彩妆品牌
2021/02/15 全球购物
生物技术毕业生自荐信
2013/10/23 职场文书
经管应届生求职信范文
2014/05/18 职场文书
社区志愿者培训方案
2014/06/10 职场文书
会议新闻稿
2015/07/17 职场文书
某药房的新员工入职告知书!
2019/07/15 职场文书
为了顺利买到演唱会的票用Python制作了自动抢票的脚本
2021/10/16 Python