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 相关文章推荐
js+css 实现遮罩居中弹出层(随浏览器窗口滚动条滚动)
Dec 11 Javascript
使用js判断当前时区TimeZone是否是夏令时
Feb 23 Javascript
javascript中的nextSibling使用陷(da)阱(keng)
May 05 Javascript
原生js实现图片层叠轮播切换效果
Feb 02 Javascript
JavaScript中ES6 Babel正确安装过程
Jul 18 Javascript
AngularJS中的表单简单入门
Jul 28 Javascript
js HTML5 Canvas绘制转盘抽奖
Sep 13 Javascript
解决Vue使用mint-ui loadmore实现上拉加载与下拉刷新出现一个页面使用多个上拉加载后冲突问题
Nov 07 Javascript
switchery按钮的使用方法
Dec 18 Javascript
微信小程序实现留言板功能
Nov 02 Javascript
three.js 实现露珠滴落动画效果的示例代码
Mar 01 Javascript
Vue.js中v-for指令的用法介绍
Mar 13 Vue.js
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&amp;MYSQL服务器配置说明
2006/10/09 PHP
php基础知识:类与对象(1)
2006/12/13 PHP
Laravel框架学习笔记(一)环境搭建
2014/10/15 PHP
PHP获取二维数组中某一列的值集合
2015/12/25 PHP
php实现的操作excel类详解
2016/01/15 PHP
最新最全PHP生成制作验证码代码详解(推荐)
2016/06/12 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
2017/11/12 PHP
WEB高性能开发之疯狂的HTML压缩
2010/06/19 Javascript
jquery动态增加删除表格行的小例子
2013/11/14 Javascript
js replace替换所有匹配的字符串
2014/02/13 Javascript
js使用removeChild方法动态删除div元素
2014/08/01 Javascript
JavaScript实现的GBK、UTF8字符串实际长度计算函数
2014/08/27 Javascript
AngularJS入门教程(二):AngularJS模板
2014/12/06 Javascript
JQuery实现简单的图片滑动切换特效
2015/11/22 Javascript
javascript中闭包(Closure)详解
2016/01/06 Javascript
关于webuploader插件使用过程遇到的小问题
2016/11/07 Javascript
详解js中call与apply关键字的作用
2016/11/21 Javascript
js将字符串中的每一个单词的首字母变为大写其余均为小写
2017/01/05 Javascript
jquery.form.js异步提交表单详解
2017/04/25 jQuery
vue中的自定义分页插件组件的示例
2018/08/18 Javascript
nodejs对mongodb数据库的增加修删该查实例代码
2020/01/05 NodeJs
Python异常处理总结
2014/08/15 Python
Python标准库之多进程(multiprocessing包)介绍
2014/11/25 Python
Python的Django框架中的Context使用
2015/07/15 Python
利用PyInstaller将python程序.py转为.exe的方法详解
2017/05/03 Python
Python读写zip压缩文件的方法
2018/08/29 Python
python实现XML解析的方法解析
2019/11/16 Python
python 实现单通道转3通道
2019/12/03 Python
python异常处理、自定义异常、断言原理与用法分析
2020/03/23 Python
想学画画?python满足你!
2020/12/24 Python
山海经纬软件测试笔试题和面试题
2013/04/02 面试题
大学生的四年学习自我评价
2013/12/13 职场文书
2014国培学习感言
2014/03/05 职场文书
体育教师求职信
2014/05/24 职场文书
高中班主任培训心得体会
2016/01/07 职场文书
2016暑期校本培训心得体会
2016/01/08 职场文书