Javascript三元运算符未按预期工作

时间:2017-09-11 14:29:26

标签: javascript jquery ternary-operator

我正处于最简单的逻辑不起作用的那一天......

当我点击链接(id=show-additional-info)以显示隐藏的内容时,我正在尝试切换图标的类:

$(document).ready(function(){

  $("#show-additional-info").on('click',  function () {

    console.log('---------------');

    var iconToggle = $('#additional-info').attr("aria-expanded");        
    var active_icon = iconToggle 
            ? 'fa fa-chevron-circle-right' 
            : 'fa fa-chevron-circle-down';

    $('#icon-toggle').attr("class", active_icon);

    // show state of trigger event, ternary operator, and affected element class
    console.log('additional-info.aria-expanded: ' + iconToggle);
    console.log('active_icon: ' + active_icon);
    console.log('icon-toggle.class: ' + $('#icon-toggle').attr("class"));
  });
});

控制台日志显示第一个状态为undefined,这是预期的,应该是falsey。通过反复点击,我看到aria-expanded属性在true和false之间切换状态。但是三元运算符并没有像我期望的那样分配值:

---------------
additional-info.aria-expanded: undefined
active_icon: fa fa-chevron-circle-down     // <-- correctly assigns state. 
icon-toggle.class: fa fa-chevron-circle-down
---------------
additional-info.aria-expanded: true
active_icon: fa fa-chevron-circle-right    // <-- correctly changes state.
icon-toggle.class: fa fa-chevron-circle-right
---------------
additional-info.aria-expanded: false
active_icon: fa fa-chevron-circle-right    // <-- should be circle down!
icon-toggle.class: fa fa-chevron-circle-right
---------------
additional-info.aria-expanded: true
active_icon: fa fa-chevron-circle-right
icon-toggle.class: fa fa-chevron-circle-right
---------------
additional-info.aria-expanded: false
active_icon: fa fa-chevron-circle-right    // <-- should be circle down!
icon-toggle.class: fa fa-chevron-circle-right

html部分按预期工作,似乎没有涉及这种意外行为,但为了完整起见,这里是:

  <i id="icon-toggle" class="fa fa-chevron-circle-right" aria-hidden="true"></i>&nbsp;<a id="show-additional-info" href="#additional-info" data-toggle="collapse">Additional Batch Info</a><br><br>
  <div>
    <ul id="additional-info" class="list-unstyled collapse">

    <!-- stuff... --->

    </ul>
  </div>

对于我的生活,我看不出我对三元运算符的错误。另一组眼睛将不胜感激!

2 个答案:

答案 0 :(得分:2)

您是否考虑过使用toggleClass()方法更轻松?

&#13;
&#13;
$(document).ready(function(){
   $("#show-additional-info").on('click',  function () {
      $("#icon-toggle").toggleClass("fa-chevron-circle-right fa-chevron-circle-down")
  });
});
&#13;
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<i id="icon-toggle" class="fa fa-chevron-circle-right" aria-hidden="true"></i>&nbsp;
<a id="show-additional-info" href="#additional-info" data-toggle="collapse">Additional Batch Info</a>
<br><br>
  <div>
    <ul id="additional-info" class="list-unstyled collapse">

    <!-- stuff... --->

    </ul>
  </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我发布问题后不久,答案就出现了:

'false' == true

我以为我正在测试一个布尔值,但它确实是一个字符串。我将代码更改为

var active_icon = iconToggle == 'true'
        ? 'fa fa-chevron-circle-right' 
        : 'fa fa-chevron-circle-down';

它按预期工作。

然而,更好的答案是不重新发明轮子,因为接受的答案显示:

$("#icon-toggle").toggleClass("fa-chevron-circle-right fa-chevron-circle-down")