足球游戏_中国足彩网¥体育资讯$

jquery中filter方法用法实例分析
来源:易贤网 阅读:797 次 日期:2015-02-10 14:18:43
温馨提示:易贤网小编为您整理了“jquery中filter方法用法实例分析”,方便广大网友查阅!

这篇文章主要介绍了jquery中filter方法用法,实例分析了filter方法的功能并对比了与find方法的区别,需要的朋友可以参考下

本文实例讲述了jquery中filter方法用法。分享给大家供大家参考。具体分析如下:

filter()方法将匹配元素集合缩减为匹配指定选择器的元素。

filter方法中的参数可以为字符串值,包含供匹配当前元素集合的选择器表达式。

一、filter的参数类型可分为两种

1、传递选择器

$('a').filter('.external')

2、传递过滤函数

代码如下:

$('a').filter(function(index) {

return $(this).hasClass('external');

})

二、Jquery中find与filter区别

1、find()会在div元素内 寻找 class为classname的元素。

2、filter()则是筛选div的class为classname的元素。

3、基本是find子元素找,filter是平级找

4、find 函数是在当前对象集合的子元素中进行查询;

5、filter 函数是对当前对象集合进行过滤, 利用过滤条件缩小范围;

6、find 函数的参数是 jQuery 选择器表达式;

7、filter 的参数也是选择器表达式, 但可以有多个条件, 用逗号分隔(逻辑或关系);

8、filter 的参数也可以是个函数, 调用函数时会自动传入 index 参数, 函数需返回 true或false 以选中或排除元素.

例如:

代码如下:

<!DOCTYPE>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gbk">

<title>Document</title>

<script>

$(function(){

$('#btn1').click(function(){

alert($('div').find('.test').html());

});

$('#btn2').click(function(){

alert($('div').filter('.test').html());

});

$('#btn3').click(function(){

alert($('div').filter('.last').html());

});

$('#btn4').click(function(){

alert($('div').filter('.first,.last').html());

});

});

</script>

</head>

<body>

<input type="button" value="test-find" id="btn1" />

<input type="button" value="test-filter" id="btn2" />

<input type="button" value="test-filter" id="btn3" />

<input type="button" value="test-filter" id="btn4" />

<div class="first">first content<span class="test">test content</span></div>

<div class="last">last<span class="test">last test content</span></div>

<div class="last">last<span>last no test content</span></div>

</body>

</html>

希望本文所述对大家的jQuery程序设计有所帮助。

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看脚本栏目
易贤网手机网站地址:jquery中filter方法用法实例分析
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标