简体   繁体   English

在jQuery中按下更改更改状态图标

[英]Change toggle state icon once pressed in Jquery

I have having trouble trying to figure out how to change my toggle state when it is pressed. 我很难弄清楚如何在按下后更改切换状态。 I am using SVG icons that are triggered by CSS and they are being called in my query scrip by .addclass. 我正在使用由CSS触发的SVG图标,并且在我的查询脚本中被.addclass调用。 Once the toggle is pressed it would like it the icon to change to a different class that will display a different icon. 按下切换开关后,它希望将其图标更改为不同的类,从而显示不同的图标。

Here is my script 这是我的剧本

 var state = false;

 $("#toggleslideButton").click(function () {
        if (!state) {
              $('#secondaryPanel').animate({width: "toggle"}, 1000);
              $('#toggleslideButton').addClass( "icon icon-exclamation" );

        state = true;
           }
        else {
              $('#secondaryPanel').animate({width: "toggle"}, 1000);
              $('#toggleslideButton').addClass( "icon icon-close" );

        state = false;
           }
 });

My HTML 我的HTML

  <aside id="secondaryPanel">
        <section id="portalInformation">test</section>
  </aside>
  <div id="control"><a href="#" id="toggleslideButton" class="icon icon-close"><span>View</span></a></div>

The icons will not display properly because the font are being used on my local machine. 图标将无法正确显示,因为我的本地计算机上正在使用该字体。 But what I am trying to do is when the menu is open i would like the toggle button to display the icon for its class icon-close. 但是我想做的是当菜单打开时,我希望切换按钮显示其类图标关闭的图标。 Then once the toggle is pressed and the menu slides back it I would like the toggle icon to read the class icon-exclamtion. 然后,一旦按下切换开关并将菜单向后滑动,我希望切换图标读取类图标的感叹号。 What am I doing wrong that it will not change? 我做错了什么,它不会改变?

jQuery has a built in function for this: .toggleClass . jQuery为此提供了一个内置函数: .toggleClass

$("#toggleslideButton").click(function () {
    $('#secondaryPanel').animate({width: "toggle"}, 1000);
    $('#toggleslideButton').toggleClass( "icon-close icon-exclamation" );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM