过滤元素集E.filter(expr)函数:
用于从匹配的元素集E中再次筛选出与filter指定表达式expr相匹配的元素集合,E 同 expr可视作同级元素关系.
元素集中查找E.find(expr)函数:
用于从匹配的元素集E的子元素中找出与find指定表达式expr相匹配的元素集合,E 同 expr可视作同父子元素关系.
查看下面代码,认清两者区别(注意:两函数运行效果一样)
*{margin:0;padding:0;}
ul li{ list-style:none;}
img {border:0 none;}
body {padding:100px;}
input[type='button']{width:100px; height:38px;display:block;position:fixed;right:20%; top:50px;}
span{display:inline-block;float:left;width:100px; height:100px; text-align:center;border:1px blue solid;margin-right:10px;}
$(function(){
$("input[type='button']").focus().click(function(){
// 使用filter()
$("span").filter('.se1,.se3').css({ // filter可带多个参数,用逗号分隔
color:'red',
border:"2px red solid",
}).text('通过过滤元素集filter()筛选匹配!')
//使用find()
//$('div').find('.se1,.se3').css({
//color:'red',
//border:"2px red solid",
//}).text('通过元素集中查找find()筛选匹配!')
})
})
子节点1子节点2子节点3
提示:你可以先修改部分代码再运行。