jquery鼠标点击事件,改变背景色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jquery-3.6.0.js"></script>
    <style>
        .paging li{
            width:60px;
            height:40px;
            line-height:40px;
            float: left;
            text-align:center;
            list-style: none;
        }
    </style>
    <script>
       $(function(){
        //    初始化div 绑定事件
        var initDiv=function(){
            $(".paging li").css("background","");
            $(".paging li").mouseover(function(){
                $("this").css("background","green")            
        })
            $(".paging li").mouseout(function(){
                 $("this").css("background","")  
            })
       };
       initDiv();
        $(".paging li").click(function(){
            initDiv();
            // 当前被点击的div改变背景色
            $(this).css("background","green");
            // 取消当前div的mouseout和mouseover事件
            $(this).unbind("mouseout")
            $(this).unbind("mouseover")
        })
    })
       
    </script>
</head>
<body>
    <ul class="paging">
        <li><<</li>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>>></li>
    </ul>
</body>
</html>

 

posted @ 2021-04-22 16:46  Hhhr  阅读(521)  评论(0编辑  收藏  举报