如何添加“活动"广告类别为"this"单击子级上的父级按钮,并在“父级"子级上切换“活动"如果再次单击该按钮,则该类 [英] How to add "active" class to "this" parent on child button is clicked and toggle "active" class if button clicked again
本文介绍了如何添加“活动"广告类别为"this"单击子级上的父级按钮,并在“父级"子级上切换“活动"如果再次单击该按钮,则该类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面给出的代码可以正常工作,除了我需要做的另一件事.
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>
控制器:
$scope.activate= function(index){
$scope.index=index;
};
以下是上述代码的作用:
Here are the things what the above code doing:
- 如果单击子级,则会将
active
类添加到父级div. - 如果单击其他项目,也会删除
active
类.
- The
active
class is added to parent div if the child is clicked. - The
active
class also get removed if you click another item.
我需要的另一项功能是:
如果再次单击同一按钮,则删除已经添加到父div
的active
类.
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
.
推荐答案
这可能有效:
$scope.activate= function(index){
if($scope.index == index)
$scope.index = -1;
else
$scope.index = index;
};
这篇关于如何添加“活动"广告类别为"this"单击子级上的父级按钮,并在“父级"子级上切换“活动"如果再次单击该按钮,则该类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文