使用 jQuery 打开和关闭

Toggle on and off with jQuery

我的目标很简单——让我的 tick/cross 在触发时打开和关闭(红色或绿色)。

我已经尝试了很多 jQuery 的变体,但由于某种原因没有任何效果。

这是 JSFiddle 的 link: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 库。

提前致谢!

使用.change() event handler on .toggle-input and check if $(this) .is(':checked').

$(document).ready(function() {
    $('.toggle-input').change(function() {
        if($(this).is(':checked')) {
            console.log('This is firing');
            $('.toggle-off').removeClass('active');
            $('.toggle-on').addClass('active');
        } else {
            $('.toggle-off').addClass('active');
            $('.toggle-on').removeClass('active');
        }
    });
});

Working JSFiddle

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

Working Demo

问题是因为点击绑定到 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;
        });
     });

实际问题解释如下。

本质上,由于单击标签还会导致在关联的输入元素上引发本机点击事件,因此如果输入元素是标签的子元素(隐式关联),则点击事件可能会被处理两次。事件委托也有点混乱。

参考:http://bugs.jquery.com/ticket/10673