[ jquery 过滤器 filter(expr|obj|ele|fn) ] 筛选出与指定表达式匹配的元素集合,这个方法用于缩小匹配的范围,用逗号分隔多个表达式

筛选出与指定表达式匹配的元素集合,这个方法用于缩小匹配的范围,用逗号分隔多个表达式

当参数是函数时,函数里面的参数解释如下:
一个函数用来作为测试元素的集合。它接受一个参数index,这是元素在jQuery集合的索引。在函数, this指的是当前的DOM元素

实例:<!DOCTYPE html>

<html lang='zh-cn'>
<head>
<title>Insert you title</title>
<meta http-equiv='description' content='this is my page'>
<meta http-equiv='keywords' content='keyword1,keyword2,keyword3'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type='text/javascript' src='./js/jquery-1.12.1.min.js'></script>
<style type="text/css">
    *{margin:0;padding:0;}
    html{font:400 15px/1.2em 'Courier New';color:#666;width:750px;margin:25px auto;}
    div > p{white-space: pre-wrap;margin:15px 0;}
    div >*{text-indent:8px;}
    .hover{color:#FF96EC;}
</style>
<script type='text/javascript'>
    $(function(){
        /*
           筛选出与指定表达式匹配的元素集合,这个方法用于缩小匹配的范围,用逗号分隔多个表达式
           当参数是函数时,函数里面的参数解释如下:
             一个函数用来作为测试元素的集合。它接受一个参数index,这是元素在jQuery集合的索引。在函数, this指的是当前的DOM元素。
        */
     // $('div').filter('.selectorDemoFirst,.selectorDemoSecond').addClass('hover') ;
$('div').filter(function(index){ if($(this).hasClass('selectorDemoFirst')) return $(this); }).addClass('hover') ; }); </script> </head> <body> <div id='demo'> <p>This is a p tag text presentation.</p> <p>This is a p tag text presentation.</p> <p>This is a p tag text presentation.</p> <div class='selectorDemoFirst' style='border:1px solid #925CE9;'> <p>This is a p tag text presentation.</p> <p>This is a p tag text presentation.</p> </div> <p>This is a p tag text presentation.</p> <div class='selectorDemoSecond' style='border:1px solid #999;margin-bottom: 45px;'> <span>This is a span tag text presentation</span> <p>This is a p tag text presentation.</p> </div> <div class='selectorDemoSecond' style='border:1px solid #925CE9;'> <span>This is a span tag text presentation</span> <p>This is a p tag text presentation.</p> <span style='padding:8px;'>This is a span tag text presentation</span> </div> <p>This is a p tag text presentation.</p> </div> </body> </html>

 

posted @ 2016-06-25 09:56  窗棂  Views(865)  Comments(0Edit  收藏  举报