Bootstrap按钮开关 - 添加另一个活动类

时间:2018-01-16 08:59:43

标签: jquery css twitter-bootstrap

如果有人第一次访问,我需要显示红色活动按钮。然后他在是和否之间切换,我需要显示另一种活跃的颜色。请看屏幕截图。

第一次

enter image description here

在是和否之间切换

enter image description here

我试过这段代码。它只是切换活动类。

$('.btn-switch').click(function() {
  var $group = $(this).closest('.form-group');
  $('.btn-switch', $group).removeClass("active");
  $(this).addClass("active");
});
.btn-switch.active {
  background: #e46a5d!important;
  color: #fff;
  font-weight: normal;
  text-shadow: none;
}

.btn {
  padding: 6px 51px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="form-group">
  <div class="btn-group btn-group-justified btn-switch-justified">
    <div class="btn-group">
      <button type="button" class="btn btn-default btn-switch active">
          <span class="goods">Yes</span>
         </button>
    </div>
    <div class="btn-group">
      <button type="button" class="btn btn-default btn-switch">
          <span class="services">No</span>
         </button>
    </div>
  </div>
</div>

我是否需要添加另一个活动类或任何其他方法来执行此操作?

Jsfiddle

2 个答案:

答案 0 :(得分:1)

如果我理解正确,这就是你想要的:

&#13;
&#13;
$('.btn-switch').click(function () {
  var $group = $(this).closest('.form-group');
  $('.btn-switch', $group).removeClass("active").addClass("switching");
  $(this).addClass("active");
});
&#13;
.btn-switch.active{
  background: #e46a5d !important;
  color: #fff;
  font-weight: normal;
  text-shadow: none;
}

.switching.active{
  background: #adf !important;
}


.btn{
  padding: 6px 51px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="form-group">
<div class="btn-group btn-group-justified btn-switch-justified">
 <div class="btn-group">
  <button type="button" class="btn btn-default btn-switch active">
  <span class="goods">Yes</span>
 </button>
</div>
 <div class="btn-group">
 <button type="button" class="btn btn-default btn-switch">
  <span class="services">No</span>
 </button>
 </div>
</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您必须根据条件切换类。

Stack Snippet

&#13;
&#13;
/* Latest compiled and minified JavaScript included as External Resource */

$('.btn-switch').click(function() {
  $('.btn-switch').removeClass("active red blue");
  if ($(this).find('span').text() == "Yes")
    $(this).addClass('active red');
  else
    $(this).addClass('active blue');
});
&#13;
/* Latest compiled and minified CSS included as External Resource*/


/* Optional theme */

@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
  margin: 10px;
}

.btn-switch.active {
  background: #e46a5d!important;
  color: #fff;
  font-weight: normal;
  text-shadow: none;
}

.btn-switch.active.red {
  background: #e46a5d!important;
}

.btn-switch.active.blue {
  background: blue !important;
}

.btn {
  padding: 6px 51px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="form-group">
  <div class="btn-group btn-group-justified btn-switch-justified">
    <div class="btn-group">
      <button type="button" class="btn btn-default btn-switch active">
        <span class="goods">Yes</span>
      </button>
    </div>
    <div class="btn-group">
      <button type="button" class="btn btn-default btn-switch">
        <span class="services">No</span>
      </button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;