【问题标题】:Change text color with jQuery mobile flip switch使用 jQuery 移动翻转开关更改文本颜色
【发布时间】:2014-02-16 04:38:24
【问题描述】:

我正在尝试根据查询移动翻转开关是打开还是关闭来更改 2 个类的文本颜色。我的印象是翻转开关只是一个复选框,打开状态被选中,关闭状态未被选中。我的 javascript 经验非常少。 有谁知道我该如何解决这个问题?

<div class="text-left">Make me blue when switch is off and grey when on </div>
<div class="text-right">Make me blue when switched is on and grey when off</div>

开关

<form>
<input type="checkbox" data-role="flipswitch" name="flip-checkbox-4" id="flip-checkbox-4" data-wrapper-class="custom-size-flipswitch">
</form>

JAVASCRIPT

<script type="text/javascript">
if($("#flip-checkbox-4").is(":checked")) {
    $(".text-left").css("color", "grey");
    $(".text-right").css("color", "blue");
} else {
    $(".text-left").css("color", "blue");
    $(".text-right").css("color", "grey")
}
</script>

【问题讨论】:

    标签: javascript jquery jquery-mobile-flipswitch


    【解决方案1】:

    你需要将它包装在一个事件中;具体来说,一个change() 事件,用于监听输入的checked 属性何时更改:

    $("#flip-checkbox-4").change(function(){
      if($("#flip-checkbox-4").is(":checked")) {
         $(".text-left").css("color", "grey");
         $(".text-right").css("color", "blue");
      } else {
         $(".text-left").css("color", "blue");
         $(".text-right").css("color", "grey")
      }
    });
    

    jsFiddle here.

    更短的方法:

    $("#flip-checkbox-4").change(function(){
       $(".text-left").css("color", this.checked ? "grey" : "blue");
       $(".text-right").css("color", this.checked ? "blue" : "grey");
    });
    

    jsFiddle here.

    【讨论】:

    • @rizzledon 不用担心 - 如果您愿意,我只是添加了一种更短的方法。
    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    相关资源
    最近更新 更多