如何在手风琴上添加加减图标

时间:2018-07-12 09:45:07

标签: jquery angularjs accordion

我在AngularJS应用程序中有一个手风琴,我正在使用jQuery对其进行扩展和折叠:一切工作正常,但是我需要在扩展和折叠中放置正/负切换图标。

我已经写了jQuery部分,但是问题在于图标在扩展时会发生变化,而在折叠时却没有变化。

这是代码:

angular.module('app', ['ui.bootstrap', 'ngSanitize', 'angular.filter']);
angular.module('app').controller('AccordionDemoCtrl', function($scope) {

  $scope.oneAtATime = true;

  $scope.groups = [{
      title: 'title 1',

      isOpen: true,
      list: ['<i>item1a</i> blah blah',
        'item2a',
        'item3a'
      ]
    },
    {
      title: 'title 2',

      list: ['item1b',
        '<b>item2b </b> blah ',
        'item3b'
      ]
    },
    {
      title: 'title 3',

    },
    {
      title: 'title 4',

    },
    {
      title: 'title 5',

    }
  ];
  $(document).ready(function() {
    $(".parents").click(function() {
      $(this).find('i').removeClass('glyphicon-minus');
      $(this).find('i').addClass('glyphicon-plus');
      $(this).next().slideToggle('fast');
      $(".childs").not($(this).next()).slideUp('fast');
    });
  });
});
.childs {
  display: none;
}
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'>

<body ng-app="app">
  <h1>Dynamic accordion: nested lists with html markup</h1>
  <div ng-controller="AccordionDemoCtrl">
    <div>
      <div ng-repeat="group in groups">
        <div class="parents"><i class="glyphicon-minus"></i> {{ group.title }}
        </div>
        {{ group.content }}
        <ul class="childs">
          <li ng-repeat="item in group.list">
            <span ng-bind-html="item"></span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js'></script>
<script src="js/index.js"></script>

1 个答案:

答案 0 :(得分:0)

需要检查哪个班级是活动的,并分别添加/删除。

 $(document).ready(function() {
    $(".parents").click(function() {

    if($(this).find("i").attr('class')=='glyphicon-plus'){
      $(this).find('i').addClass('glyphicon-minus');
      $(this).find('i').removeClass('glyphicon-plus');
     }
     else{
      $(this).find('i').removeClass('glyphicon-minus');
      $(this).find('i').addClass('glyphicon-plus');
      }
      $(this).next().slideToggle('fast');
      $(".childs").not($(this).next()).slideUp('fast');
    });
  });

<body ng-app="app">
  <h1>Dynamic accordion: nested lists with html markup</h1>
  <div ng-controller="AccordionDemoCtrl">
    <div>
      <div ng-repeat="group in groups">
        <div class="parents"><i class="glyphicon-plus"></i> {{ group.title }}
        </div>
        {{ group.content }}
        <ul class="childs">
          <li ng-repeat="item in group.list">
            <span ng-bind-html="item"></span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</body>