onclick点击事件,切换图标的颜色

收藏按钮 粉色、白色、切换

.Collect_Btn {
    float: left;
    color: white;
    font-size: 14px;
    margin-right: 20px;
}
.Collect_BtnCur {
    float: left;
    color: #F6524E;
    font-size: 14px;
    margin-right: 20px;
}

<a  href="#" class="Collect_Btn"></a>

<script>
    // <!-- 收藏按钮 粉色、白色、切换-->
    $('.Collect_Btn').on('click', function () {
        if($(this).hasClass('Collect_BtnCur')) {
            $(this).attr("class","Collect_Btn")
        } else {
            $(this).attr("class","Collect_BtnCur")
        }
    })
</script>

案例二: 是否隐藏

image.png
<div class="td_ShowHide">
    <div class="td_ShowHide_Circle"></div>
</div>
.td_ShowHide {
  position: relative;
  background-color: rgba(0, 0, 0, 0.3);
  height: 20px;
  width: 80px;
  text-align: center;
  line-height: 20px;
  border-radius: 10px;
  cursor: pointer;
  transition:All 0.4s ease-in-out;
}
.td_ShowHide::after {
  display: block;
  content: '隐藏中'
}
.td_ShowHide.cur {
  background-color: rgba(182, 109, 255, 1);
  color: #fff;
}
.td_ShowHide.cur::after {
  display: block;
  content: '显示中'
}
.td_ShowHide .td_ShowHide_Circle {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid #999;
  top: 1px;
  left: 1px;
  background: #fff;
  transition:All 0.4s ease-in-out;

}
.td_ShowHide.cur .td_ShowHide_Circle {
  left: 61px;
  transition:All 0.4s ease-in-out;
}
    // <!-- 隐藏显示按钮 切换 -->
    $('.td_ShowHide').on('click', function () {
      if ($(this).hasClass('cur')) {
        $(this).attr("class", "td_ShowHide")
      } else {
        $(this).attr("class", "td_ShowHide cur")
      }
    })
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。