1、清空所有,给当前添加/2、清空上一个,给当前添加。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="../ajax/ajaxdemo/js/jquery-2.1.4.min.js" ></script>
        <style>
            li{width: 20px;height: 20px;margin: 0 20px;background: yellowgreen;list-style: none;padding: 0;float: left;text-align: center;color: white;}
            .active{background: yellow;}
        </style>
    </head>
    <body>
        <ul>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </body>
</html>
<script>
//    定义一个原来的初始的。
    var oldli=$('li')[0];
    $('li').bind('click',function(){
//        请全部给当前添加
//        $('li').removeClass('active');
//        $(this).addClass('active');
        
//        清除上一个,给当前添加
        $(oldli).removeClass('active');
        oldli=this;
        $(this).addClass('active');
    })
</script>

 

posted @ 2017-11-08 14:48  盖大楼  阅读(187)  评论(0编辑  收藏  举报