jquery元素筛选和事件

1>元素筛选方法


       $(function(){

         //jquery的遍历方法 .each()
         $('ul').children().each(function(index,ele){

                console.log(index);
            console.log(ele);

            var isDanger = $(this).hasClass('danger');  //class包含danger

            if(isDanger){
               $(this).css('color','red'); //字体设置红色

            }else{
//             $(this).css('font-size','20px');
            }

         });
            
         console.log($('ul').children('.danger'));
         console.log($('li'));
         console.log($('a').parent());
            console.log('------------------------');
         console.log($('li').last().prev());
         console.log($('li').prevAll());
         console.log($('li').siblings('li'));

         //siblings

         console.log($('#anchor').siblings('a'));

         $('li').hover(function(){   //鼠标悬停,class增加active,也就是字体设置为50px,毗邻元素移除active
            $(this).addClass('active').siblings('li').removeClass('active');
         })

      })

2>轮播图案例

    <style type="text/css">
         *{padding: 0;margin: 0;}
         ul,ol{list-style: none;}

         #box{width: 650px;height: 250px;margin: 100px auto 0;background: red;overflow: hidden;position: relative;}
         #box ul{height: 250px;position: relative;z-index: 1;}
         #box ol{height: 30px;position: absolute;z-index: 2;bottom: 0;right: 0;}

         #box>ul>li{
            position: absolute;
            top:0;
            left: 0;
         }

         #box>ol>li{
            float: left;
            width: 20px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            border: 1px solid white;
            background: gray;
            margin-right: 5px;
         }
         #box>ol>li:hover{
            cursor: pointer;
         }

         #box li.active{
            padding: 2px;
            color: orange;
            margin-top: -4px;
            border: 1px solid orange;
         }

    </style>
</head>
<body>

    <div id="box">
        <ul>
            <li style="z-index: 1"><a href="#"><img src="./images/01.jpg"></a></li>
            <li><a href="#"><img src="./images/02.jpg"></a></li>
            <li><a href="#"><img src="./images/03.jpg"></a></li>
            <li><a href="#"><img src="./images/04.jpg"></a></li>
            <li><a href="#"><img src="./images/05.jpg"></a></li>
        </ul>
        <ol>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ol>
    </div>

</body>

<script src="jquery-3.3.1.min.js"></script>

<script type="text/javascript">

    $(function(){

        var index=0;
        $('#box>ol>li').hover(function(){
            index++;

            //更改下标的class
            $(this).addClass('active').siblings('li').removeClass('active');

            //根据下标设置对应的图片显示,index控制显示优先级
            $('#box>ul>li').eq($(this).index()).css({left:650,'z-index':index}).animate({left:0},1000);
//            $('#box>ul>li').eq($(this).index()).css({'z-index':index})
            
        })
        
    })


</script>

3>jquery常用事件



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值