jquery 动态点击li标签切换背景颜色

废话不多讲老规矩先上图 看效果:


<script type="text/javascript" src="/jquery/jquery.js"></script> 改为你本地项目的js路径

<!DOCTYPE html>

<html>
<head>
  <script type="text/javascript" src="/jquery/jquery.js"></script><!-- 你本地路径 --->
</head>
<style>
.currentli {
            color:#186ecc;
            background-color:#fff;
        }
</style>
<body>
  <ul id="gmenu">
  <li class="selected">Hello Again</li>
  <li class="selected">Hello Again</li>
  <li>And Again</li>
</ul>
<script>

 $("#gmenu p").click(function () {
                    $(this).addClass("currentli").siblings().removeClass();
                })
</script>

</body>

</html>

如果以上没有起作用可是尝试以下:

    $(".gmenu ul li").click(function () {
          $(".gmenuulul li").removeClass();
          $(this).addClass("currentli");
    })


猜你喜欢

转载自blog.csdn.net/nicholas_long/article/details/80419576
今日推荐