jQuery10(过滤器)

过滤器

关键词:first
语法实例:$('ul li:first').css('backgroundColor','yellow');
意义:ul标签中的第一个元素

关键词:last
语法实例:$('ul li:last').css('backgroundColor','yellow');
意义:ul标签中的最后一个元素

关键词:even
语法实例:$('ul li:even').css('backgroundColor','yellow');
意义:ul标签中的奇数行发生变化.
注意:even意义为偶数

关键词:odd
语法实例:$('ul li:odd').css('backgroundColor','yellow');
意义:ul标签中的偶数行发生变化.
注意:odd意义为奇数

实例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.8.3.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#btn').click(function () {
                //第一个元素
                //$('ul li:first').css('backgroundColor', 'red');
                //最后一个元素
                //$('ul li:last').css('backgroundColor', 'red');
                //偶数,显示效果为奇数行发生变化
                $('ul li:even').css('backgroundColor', 'red');
                //奇数,显示效果为偶数行改变
                //$('ul li:odd').css('backgroundColor', 'red');
            });
        });
    </script>
</head>
<body>
    <input type="button" name="name" value="显示效果 " id='btn' />
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>

关键词:eq(num)
语法实例:$('div:eq(3)').css('backgroundColor','red');
意义:从div的索引3开始

关键词:lt(num)
语法实例:$('div:lt(3)').css('backgroundColor','red');
意义:从索引小于3的div开始

关键词:gt(num)
语法实例:$('div:gt(3)').css('backgroundColor','red');
意义:从索引大于3的div开始

关键词:组合
语法实例:$('div:gt(3):lt(2)').css('backgroundColor','red');
意义:从索引大于3小于2的div开始,请注意,这里查找完索引大于3后,在当前位置索引变为0.

实例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.8.3.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#btn').click(function () {
                //索引为3的div 从0开始
                //$('div:eq(3)').css('backgroundColor', 'red');
                //索引小于3 的div
                //$('div:lt(3)').css('backgroundColor', 'red');
                //索引大于3的div
                //$('div:gt(3)').css('backgroundColor', 'red');
                //索引大于3小于2的div 索引大于3 查完后,索引变为0重新开始查询索引小于2
                $('div:gt(3):lt(2)').css('backgroundColor', 'red');
            });
        });
    </script>
    <style type="text/css">
        div
        {
            width: 300px;
            height: 20px;
            background-color: Green;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <input id='btn' type="button" name="name" value="显示效果" />
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值