juqery 学习之四 筛选过滤


Posted in Javascript onNovember 30, 2010

eq(index)

获取第N个元素
这个元素的位置是从0算起。

Reduce the set of matched elements to a single element.
The position of the element in the set of matched elements starts at 0 and goes to length - 1.

返回值

jQuery

参数

index (Integer) :元素在jQuery对象中的索引

示例

获取匹配的第二个元素

HTML 代码:

<p> This is just a test.</p> <p> So is this</p>

jQuery 代码:

$("p").eq(1)

结果:

[ <p> So is this</p> ]
--------------------------------------------------------------------------------------------------------------

hasClass(class)

检查当前的元素是否含有某个特定的类,如果有,则返回true。
这其实就是 is("." + class)。

Checks the current selection against a class and returns true, if at least one element of the selection has the given class.
This is an alternative to is("." + class).

返回值

Boolean

参数

class (String) : 用于匹配的类名

示例

给包含有某个类的元素进行一个动画。

HTML 代码:

<div class="protected"></div><div></div>

jQuery 代码:

$("div").click(function(){
  if ( $(this).hasClass("protected") )
    $(this)
      .animate({ left: -10 })
      .animate({ left: 10 })
      .animate({ left: -10 })
      .animate({ left: 10 })
      .animate({ left: 0 });
});

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

filter(expr)

筛选出与指定表达式匹配的元素集合。
这个方法用于缩小匹配的范围。用逗号分隔多个表达式

Removes all elements from the set of matched elements that do not match the specified expression(s).
This method is used to narrow down the results of a search. Provide a comma-separated list of expressions to apply multiple filters at once.

返回值

jQuery

参数

expr (Expression) : 表达式

示例

保留带有select类的元素

HTML 代码:

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

jQuery 代码:

$("p").filter(".selected")

结果:

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

保留第一个以及带有select类的元素

HTML 代码:

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

jQuery 代码:

$("p").filter(".selected, :first")

结果:

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

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

filter(fn)

筛选出与指定函数返回值匹配的元素集合
这个函数内部将对每个对象计算一次 (正如 '$.each'). 如果调用的函数返回false则这个元素被删除,否则就会保留。

Removes all elements from the set of matched elements that does not match the specified function.
The function is called with a context equal to the current element (just like '$.each'). If the function returns false, then the element is removed - anything else and the element is kept.

返回值

jQuery

参数

fn (Function) : 传递进filter的函数

示例

保留子元素中不含有ol的元素。

HTML 代码:

<p><ol><li>Hello</li></ol></p><p>How are you?</p>

jQuery 代码:

$("p").filter(function(index) {
  return $("ol", this).length == 0;
});

结果:

[ <p>How are you?</p> ]

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

is(expr)

用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true。
如果没有元素符合,或者表达式无效,都返回'false'. 'filter' 内部实际也是在调用这个函数,所以,filter()函数原有的规则在这里也适用。

Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
If no element fits, or the expression is not valid, then the response will be 'false'. 'filter' is used internally, therefore all rules that apply there apply here, as well.

返回值

Boolean

参数

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

示例

由于input元素的父元素是一个表单元素,所以返回true。

HTML 代码:

<form><input type="checkbox" /></form>

jQuery 代码:

$("input[type='checkbox']").parent().is("form")

结果:

true

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

map(callback)

将一组元素转换成其他数组(不论是否是元素数组)
你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用'$.map()'来方便的建立。

Translate a set of elements into another set of values (which may, or may not, be elements).
You could use this to build lists of values, attributes, css values - or even perform special, custom, selector transformations. This is provided as a convenience method for using '$.map()'.

返回值

jQuery

参数

callback (Function) : 给每个元素执行的函数

示例

把form中的每个input元素的值建立一个列表。

HTML 代码:

<p><b>Values: </b></p>
<form>
  <input type="text" name="name" value="John"/>
  <input type="text" name="password" value="password"/>
  <input type="text" name="url" value="http://ejohn.org/"/>
</form>

jQuery 代码:

$("p").append( $("input").map(function(){
  return $(this).val();
}).get().join(", ") );

结果:

[ <p>John, password, http://ejohn.org/</p> ]

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

not(expr)

删除与指定表达式匹配的元素

Removes elements matching the specified expression from the set of matched elements.

返回值

jQuery

参数

expr (String, DOMElement, Array<DOMElement>) : 一个表达式、一个元素或者一组元素

示例

从p元素中删除带有 select 的ID的元素

HTML 代码:

<p>Hello</p><p id="selected">Hello Again</p>

jQuery 代码:

$("p").not( $("#selected")[0] )

结果:

[ <p>Hello</p> ]
-------------------------------------------------------------------------------------------------------------------------

slice(start,[end])

选取一个匹配的子集
与原来的slice方法类似

Selects a subset of the matched elements.
Behaves exactly like the built-in Array slice method.

返回值

jQuery

参数

start (Integer) :开始选取子集的位置。第一个元素是0.如果是负数,则可以从集合的尾部开始选起。

end (Integer) : (可选) 结束选取自己的位置,如果不指定,则就是本身的结尾。

示例

选择第一个p元素

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>

jQuery 代码:

$("p").slice(0, 1).wrapInner("<b></b>");

结果:

[ <p><b>Hello</b></p> ]

选择前两个p元素

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>

jQuery 代码:

$("p").slice(0, 2).wrapInner("<b></b>");

结果:

[ <p><b>Hello</b></p>,<p><b>cruel</b></p> ]

只选取第二个p元素

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>

jQuery 代码:

$("p").slice(1, 2).wrapInner("<b></b>");

结果:

[ <p><b>cruel</b></p> ]

只选取第二第三个p元素

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>

jQuery 代码:

$("p").slice(1).wrapInner("<b></b>");

结果:

[ <p><b>cruel</b></p>, <p><b>World</b></p> ]

Selects all paragraphs, then slices the selection to include only the third element.

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>

jQuery 代码:

$("p").slice(-1).wrapInner("<b></b>");

结果:

[ <p><b>World</b></p> ]
Javascript 相关文章推荐
用正则表达式 动态创建/增加css style script 兼容IE firefox
Mar 10 Javascript
jQuery对象[0]是什么含义?
Jul 31 Javascript
理解JavaScript中的对象 推荐
Jan 09 Javascript
jquery星级插件、支持页面中多次使用
Mar 25 Javascript
js实现漂浮回顶部按钮实例
May 06 Javascript
jquery点击缩略图切换视频播放特效代码分享
Sep 15 Javascript
JS实现不规则TAB选项卡效果代码
Sep 16 Javascript
解决vue+webpack打包路径的问题
Mar 06 Javascript
微信小程序实现的一键复制功能示例
Apr 24 Javascript
关于vue里页面的缓存详解
Nov 04 Javascript
jQuery实现二级导航菜单的示例
Sep 30 jQuery
前端canvas中物体边框和控制点的实现示例
Aug 05 Javascript
juqery 学习之四 筛选查找
Nov 30 #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
You might like
php输出xml必须header的解决方法
2014/10/17 PHP
教大家制作简单的php日历
2015/11/17 PHP
Yii2.0预定义的别名功能小结
2016/07/04 PHP
关于javascript中的parseInt使用技巧
2009/09/03 Javascript
IE iframe的onload方法分析小结
2010/01/07 Javascript
javascript事件模型介绍
2016/05/31 Javascript
总结Node.js中的一些错误类型
2016/08/15 Javascript
Javascript生成带参数的二维码示例
2016/10/10 Javascript
jQuery延迟执行的实现方法
2016/12/21 Javascript
JavaScript简单验证表单空值及邮箱格式的方法
2017/01/20 Javascript
ES6学习教程之对象的扩展详解
2017/05/02 Javascript
Angular 4依赖注入学习教程之组件服务注入(二)
2017/06/04 Javascript
vue解决跨域问题(推荐)
2020/11/10 Javascript
python 拷贝特定后缀名文件,并保留原始目录结构的实例
2018/04/27 Python
python Pexpect 实现输密码 scp 拷贝的方法
2019/01/03 Python
django创建最简单HTML页面跳转方法
2019/08/16 Python
Pytorch训练过程出现nan的解决方式
2020/01/02 Python
tensorflow 获取checkpoint中的变量列表实例
2020/02/11 Python
浅谈Python中os模块及shutil模块的常规操作
2020/04/03 Python
使用tensorflow进行音乐类型的分类
2020/08/14 Python
thinkphp5 路由分发原理
2021/03/18 PHP
HTML5 UTF-8 中文乱码的解决方法
2013/11/18 HTML / CSS
新英格兰最大的特色礼品连锁店:The Paper Store
2018/07/23 全球购物
美国折扣地毯销售网站:Rugs.com
2020/03/27 全球购物
90后毕业生的求职信范文
2013/09/21 职场文书
办公室前台的岗位职责
2013/12/20 职场文书
测试工程师程序员求职信范文
2014/02/20 职场文书
高中军训感想800字
2014/02/23 职场文书
火锅店的活动方案
2014/08/15 职场文书
2014年体育工作总结
2014/11/24 职场文书
2015年五四青年节活动总结
2015/02/10 职场文书
入党转正介绍人意见
2015/06/03 职场文书
2019年个人工作总结范文(3篇)
2019/08/27 职场文书
使用Python脚本对GiteePages进行一键部署的使用说明
2021/05/27 Python
Python基于百度AI实现抓取表情包
2021/06/27 Python
讨论nginx location 顺序问题
2022/05/30 Servers