jquery在2个多个类之间切换

时间:2017-06-02 10:52:54

标签: javascript jquery html

我正在尝试在jquery中的2个多个类之间切换。我想让它从点击自己的图标变为字体真棒图标。它只适用于一个类来改变,但是当我想要它改变多个类时,我无法让它工作。

jQuery(document).ready(function () {
var $menu = 'icon '+'icon-menu';
var $close = 'fa '+'fa-close';

$('.icon-toggle-menu').click(function (evt) {
    $('.nav-screen').fadeToggle();
    $($menu, $close).toggleClass($menu +' '+ $close);
    $('.nav-logo-black').fadeToggle();
    $('.nav-icons').toggleClass('nav-fixed');
    $('.nav').toggleClass('nav-padding');
})
});
<nav class="nav">
    <a class="nav-logo-black" href="index.html">
        <img src="assets/img/logo/forcitx2.png" alt="forcit-logo-banner">
    </a>
    <ul class="pull-right nav-icons">
        <li><a class="icon-chat" href="#"><span class="icon icon-message"></span></a></li>
        <li><a class="icon-toggle-menu" href="#"><span class="icon icon-menu"></span></a></li>
    </ul>
</nav>

1 个答案:

答案 0 :(得分:3)

好的,我将尝试修复您的代码。即使它并不完全清楚你的目标是什么:

&#13;
&#13;
$(function() {
  $('.icon-toggle-menu').click(function(evt) {
    //$('.nav-screen').fadeToggle(); Missing in HTML
    $('.icon.icon-menu, .fa.fa-close').toggleClass('icon icon-menu fa fa-close');
    $('.nav-logo-black').fadeToggle();
    $('.nav-icons').toggleClass('nav-fixed');
    $('.nav').toggleClass('nav-padding');
  })
});
&#13;
.icon {
  font-weight: bold;
}

.fa.fa-close {
  color: red;
}

.icon.icon-menu {
  color: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav">
    <a class="nav-logo-black" href="index.html">
        Logo
    </a>
    <ul class="pull-right nav-icons">
        <li><a class="icon-chat" href="#"><span class="icon icon-message">Icon chat</span></a></li>
        <li><a class="icon-toggle-menu" href="#"><span class="icon icon-menu">Icon toggle menu (click me)</span></a></li>
    </ul>
</nav>
&#13;
&#13;
&#13;