如何添加“活动"类到“这个"单击子按钮上的父级并切换“活动"如果再次单击按钮,则为类 [英] How to add "active" class to "this" parent on child button is clicked and toggle "active" class if button clicked again

查看:19
本文介绍了如何添加“活动"类到“这个"单击子按钮上的父级并切换“活动"如果再次单击按钮,则为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了我需要的另一件事之外,下面给出的代码工作正常.

HTML:

<div class="item" ng-repeat="[0,1,2]中的单元格" data-ng-class="{active:index=='{{$index}}'}"><button data-ng-click="activate('{{$index}}')">激活我</button>

控制器:

 $scope.activate= function(index){$scope.index=index;};

以上代码的作用如下:

  • 如果子项被点击,active 类会被添加到父 div 中.
  • 如果您点击另一个项目,active 类也会被删除.

我需要的一项附加功能是:如果再次单击同一按钮,则删除已添加到父 divactive 类.

解决方案

这可能有效:

$scope.activate= function(index){if($scope.index == index)$scope.index = -1;别的$scope.index = 索引;};

The below given code is just working fine apart from one more thing I need.

HTML:

<div class="item" ng-repeat="cell in [0,1,2]" data-ng-class="{active:index=='{{$index}}'}">
    <button data-ng-click="activate('{{$index}}')">Activate Me</button>
</div>

Controller:

  $scope.activate= function(index){
      $scope.index=index;
  };

Here are the things what the above code doing:

  • The active class is added to parent div if the child is clicked.
  • The active class also get removed if you click another item.

The one additional function that I need is: If the same button is clicked again then remove the active class that's already added to parent div.

解决方案

This might work:

$scope.activate= function(index){
      if($scope.index == index)
          $scope.index = -1;
      else
          $scope.index = index;
};

这篇关于如何添加“活动"类到“这个"单击子按钮上的父级并切换“活动"如果再次单击按钮,则为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆