【问题标题】:Display background color on li which is generating from ng-repeat在从 ng-repeat 生成的 li 上显示背景颜色
【发布时间】:2018-04-27 10:03:48
【问题描述】:

下面的代码用图片和文字动态显示li的列表。

<ul class="presale">
                    <li ng-repeat="(k,v) in value | groupBy : 'presales_name'" ng-click="showData(k.split(' ').join('-').replace('(','').replace(')',''), $index)">
                        <div class="imgDiv">
                            <img src="/images/phones/sub-family/xs/{{k.split(' ').join('-').replace('(','').replace(')','-').toLowerCase()}}.png">
                        </div>
                        <p><a>{{k}}</a></p>
                    </li>
</ul>


$scope.showData = function (item, index) {              

}

我需要添加一个代码以在用户选择的特定 li 上显示背景颜色,然后从先前选择的 li 中隐藏背景颜色。

【问题讨论】:

    标签: jquery html css angularjs


    【解决方案1】:

    您可以执行以下操作

    JS 文件

    var app = angular.module('app', []);
    
    app.controller('NavigationController', function ($scope) 
    {
        // Must use a wrapper object, otherwise "activeItem" won't work
        $scope.states = {};
        $scope.states.activeItem = 'item1';
        $scope.items = [{
            id: 'item1',
            title: 'Home'
        }, {
            id: 'item2',
            title: 'Public Rooms'
        }, {
            id: 'item3',
            title: 'My Rooms'
        }];
    });
    

    HTML 文件

    <body ng-app="app">
        <div ng:controller="NavigationController">
            <p>
                Your active menu item: <span class="highlightOne">{{states.activeItem}}</span>
            </p>
            <ul class="navigation">
                <li ng-repeat="item in items" class="item" ng-class="{'active': item.id == states.activeItem}" ng-click="states.activeItem=item.id">{{item.title}}</li>
            </ul>
        </div>
    </body>
    

    CSS 文件

    ul.navigation li
    {
        background: #f6f6f6;
        padding: 10px;
        margin: 2px 0;
        display: block;
        cursor: pointer;
    }
    
        ul.navigation li.active
        {
            background: blue;
            color: #f6f6f6;
            display: block;
            padding: 10px;
        }
    
    .highlightOne
    {
        background: orange;
        font-size: 125%;
        margin: 5px;
        padding: 5px;
    }
    
    .navigation
    {
        display: block;
        background: #666;
        margin: 10px;
        padding: 10px;
        width: 40%;
    }
    
    p
    {
        margin: 10px 0 20px;
    }
    

    代码参考请查看“https://codepen.io/SkiWether/pen/lzjAq”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多