Heim > Fragen und Antworten > Hauptteil
如图所示,上面的5个块,用ng-repeat循环输出,怎样实现当点击某一块(如块2)时,该块的背景颜色变为红色?其他的块的颜色不变。
谢谢指点。
大家讲道理2017-05-15 17:00:27
方法太多,ng-click,ng-class,directive实现都可以啊
这是直接写的方法,仅供参考
html:
<p>
<span ng-class="{'selected':selected==s}" ng-repeat="s in list" ng-bind="s" ng-click="changeStatus(s)"></span>
</p>
js
$scope.list = [1,2,3,4,5];
$scope.changeStatus = function(index){
$scope.selected = index;
}
phpcn_u15822017-05-15 17:00:27
给你个现成的代码:
HTML
<p class='options'>
<span class='option' ng-class="{'selected':s._selected}" ng-repeat="s in orderStatus" ng-bind="s.l" ng-click="clickStatus(s)"></span>
</p>
JS
$scope.clickStatus = function(prop) {
prop._selected = !prop._selected;
};
CSS
.option{display:inline-block;border:1px solid green;padding:.25em;margin:.5em .5em 0 0;}
.option.selected{background:green;color:white;}
我正打算写个directive做这个