<html>
<head>
//filter :过滤
//not : filter的反义词
<script>
$ (function() {
$('div') .filter(' . box') . css ('background', 'red') ;
//$('div') .not(' .box') .css ('background', 'red');
}) ;
</script>
</head>
<body>
<div class="box">div1</div>
<div>div2</div>
</body>
</html>
代码解读:
从所有DIV中使用filter(' . box') ,选出该标签有.box类属性的DIV,修改背景颜色为红色。not()表示没有该属性,filter的反义词。
与has()的区别:后代元素中含有指定标签用has()。
<div class="box">divl<span>span</ span></div>
//has :包含
<script>
$ (function() {
$('div') .has('span') .css ('background', 'red');
}) ;
</script>
总结:filter()是元素自身的含有,可用来过滤,与not()相反,而has()是指元素子元素内含有,不是指自身的含有,相当于过滤子元素。
|