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 相关文章推荐
Highslide.js是一款基于js实现的网页中图片展示插件
Mar 30 Javascript
用unescape反编码得出汉字示例
Apr 24 Javascript
Javascript基础教程之数组 array
Jan 18 Javascript
jQuery自定义滚动条完整实例
Jan 08 Javascript
jquery采用oop模式class类的使用示例
Jan 22 Javascript
jQuery+canvas实现的球体平抛及颜色动态变换效果
Jan 28 Javascript
三个js循环的关键字示例(for与while)
Feb 16 Javascript
深入理解requestAnimationFrame的动画循环
Sep 20 Javascript
JavaScript中从setTimeout与setInterval到AJAX异步
Feb 13 Javascript
php main 与 iframe 相互通讯类(js+php同域/跨域)
Sep 14 Javascript
基于vue打包后字体和图片资源失效问题的解决方法
Mar 06 Javascript
layui下拉框获取下拉值(select)的例子
Sep 10 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 学习路线与时间表
2010/02/21 PHP
PHP生成UTF8文件的方法
2010/05/15 PHP
fleaphp crud操作之findByField函数的使用方法
2011/04/23 PHP
php+html5+ajax实现上传图片的方法
2016/05/14 PHP
浅谈PHP中关于foreach使用引用变量的坑
2016/11/14 PHP
tp5.1 实现setInc字段自动加1
2019/10/18 PHP
jquery简单体验
2007/01/10 Javascript
选择TreeView控件的树状数据节点的JS方法(jquery)
2010/02/06 Javascript
JS 类型转换常见方法小结
2010/05/31 Javascript
javascript实现复选框超过限制即弹出警告框的方法
2015/02/25 Javascript
JS实现表单验证功能(验证手机号是否存在,验证码倒计时)
2016/10/11 Javascript
详解angularJs模块ui-router之状态嵌套和视图嵌套
2017/04/28 Javascript
关于vue.js组件数据流的问题
2017/07/26 Javascript
微信小程序开发教程之增加mixin扩展
2017/08/09 Javascript
nodejs实现简单的gulp打包
2017/12/21 NodeJs
Webpack4 使用Babel处理ES6语法的方法示例
2019/03/07 Javascript
VSCode搭建Vue项目的方法
2020/04/30 Javascript
jquery实现淡入淡出轮播图效果
2020/12/13 jQuery
[46:40]VGJ.T vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
[48:27]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
[36:20]KG vs SECRET 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
9种python web 程序的部署方式小结
2014/06/30 Python
Python中装饰器学习总结
2018/02/10 Python
python通过tcp发送xml报文的方法
2018/12/28 Python
Python 堆叠柱状图绘制方法
2019/07/29 Python
Django 响应数据response的返回源码详解
2019/08/06 Python
在Python中使用MongoEngine操作数据库教程实例
2019/12/03 Python
解决python 在for循环并且pop数组的时候会跳过某些元素的问题
2020/12/11 Python
某公司Java工程师面试题笔试题
2016/03/27 面试题
往来会计岗位职责
2013/12/19 职场文书
大二自我鉴定
2014/01/31 职场文书
环境保护标语
2014/06/20 职场文书
工作失职造成投诉的检讨书范文
2014/10/05 职场文书
新娘婚礼答谢词
2015/09/29 职场文书
八年级作文之感恩
2019/11/22 职场文书
JS前端可扩展的低代码UI框架Sunmao使用详解
2022/07/23 Javascript