使用jQuery打开和关闭

时间:2015-09-16 09:38:12

标签: jquery label

我的目标很简单 - 当切换开关时,我的勾选/交叉切换开启和关闭(红色或绿色)。

我尝试了很多jQuery的变种,但由于某些原因,没有任何作用。

以下是JSFiddle的链接:http://jsfiddle.net/t7nep473/

HTML:

<div class="three columns"> 
  <label class="toggle">
    <i class="fa fa-close toggle-off active"></i>
    <input type="checkbox" class="toggle-input">
    <div class="toggle-controller default-success"></div>
    <i class="fa fa-check toggle-on"></i>
  </label><!-- END .toggle -->
</div><!-- END .three.columns -->

CSS:

.toggle {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  display: block;
  height: auto;
  width: 7.5rem;
  margin: auto;
  cursor: pointer;
}
.toggle-input {
  display: none;
  margin: 0;
}
.toggle-off,
.toggle-on {
  height: 1.8125rem;
  width: 1.8125rem;
  color: rgba(46, 45, 44, 0.1);
  vertical-align: top;
  text-align: center;
  line-height: 1.8125rem !important;
}
.toggle-input:checked + .toggle-controller.default-success {
  border: 0.125rem solid rgba(108, 211, 61, 0.75);
  background: rgba(108, 211, 61, 0.375);
}
.toggle-input:checked + .toggle-controller.default-success:after {
  left: 1.5625rem;
}
.toggle-controller.default-success {
  position: relative;
  display: inline-block;
  height: 1.5625rem;
  width: 3.125rem;
  border: 0.125rem solid rgba(46, 45, 44, 0.05);
  -webkit-border-radius: 1.5625rem;
  -moz-border-radius: 1.5625rem;
  border-radius: 1.5625rem;
  -webkit-box-shadow: inset 0 0 0.1875rem rgba(46, 45, 44, 0.25);
  -moz-box-shadow: inset 0 0 0.1875rem rgba(46, 45, 44, 0.25);
  box-shadow: inset 0 0 0.1875rem rgba(46, 45, 44, 0.25);
  background: rgba(46, 45, 44, 0.025);
  -webkit-transition: all 0.25s ease;
  -moz-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}
.toggle-controller.default-success:after {
  position: absolute;
  top: 0;
  left: 0;
  content: '';
  display: block;
  height: 1.5625rem;
  width: 1.5625rem;
  -webkit-border-radius: 1.5625rem;
  -moz-border-radius: 1.5625rem;
  border-radius: 1.5625rem;
  -webkit-box-shadow: 0 0.0625rem 0.125rem rgba(46, 45, 44, 0.2);
  -moz-box-shadow: 0 0.0625rem 0.125rem rgba(46, 45, 44, 0.2);
  box-shadow: 0 0.0625rem 0.125rem rgba(46, 45, 44, 0.2);
  background: white;
  -webkit-transition: all 0.25s ease;
  -moz-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}
.toggle-off.active {color:red;}
.toggle-on.active {color:green;}

jQuery的:

$(document).ready(function() {
    $('.toggle').click(function() {
        if($(this).find('.toggle-off').hasClass('active')) {
            console.log('This is firing');
            $(this).find('.toggle-off').removeClass('active');
            $(this).find('.toggle-on').addClass('active');
        } else {
            $(this).find('.toggle-off').addClass('active');
            $(this).find('.toggle-on').removeClass('active');
        }
    });
});

注意:我正在使用Font Awesome Library。

提前致谢!

3 个答案:

答案 0 :(得分:7)

isb上使用.change()事件处理程序并检查是否.toggle-input .is(':checked')

$(this)

Working JSFiddle

答案 1 :(得分:2)

$(document).ready(function() {
  $('.toggle').change(function() {
        $(this).find('.toggle-on').toggleClass('active');
        $(this).find('.toggle-off').toggleClass('active');
  });
});

Working Demo

答案 2 :(得分:1)

问题是因为点击被绑定到label

我们可以通过添加return false;

来解决此问题
  $(document).ready(function() {
        $('.toggle').click(function() {
            if($(this).find('.toggle-off').hasClass('active')) {
                console.log('This is firing');
                $(this).find('.toggle-off').removeClass('active');
                $(this).find('.toggle-on').addClass('active');
            } else {
                $(this).find('.toggle-off').addClass('active');
                $(this).find('.toggle-on').removeClass('active');
            }
           return false;
        });
     });

实际问题解释如下。

基本上,因为单击标签也会导致在关联的输入元素上抛出本机单击事件,如果input元素是标签的子级(隐式关联),则可以处理两次单击事件。还有一些事件委托也可以。

参考:wake up