点击后按钮更改颜色 - 焦点无法正常工作

时间:2018-02-09 08:59:24

标签: html css typescript angular5

我想在导航面板中创建按钮,在选择页面后,将采用不同的颜色,如果我点击下一页返回旧颜色。我使用angulara的材料设计。我试图通过以下方式在css中进行颜色更改。单击更改后的颜色,但单击页面上的上下文时返回旧颜色。

.btn {
  padding: 5px 20px  0px 20px;
  font-size: 16px;
  font-weight: 700;
}
.btn:hover {
   opacity: 1;
   transition: all 0.2s ease-out;
   color: rgb(255,100,100);
 }
.btn:focus{
      color: rgb(255,102,100);
    }
.btn:active {
  color: rgb(255,102,100);
}
<button mat-button class="mainPageButton btn" id="btnHousing" routerLink="/home">Strona Główna</button>

1 个答案:

答案 0 :(得分:1)

尝试在角度5

中使用此代码

这是小提琴:http://jsfiddle.net/gy2an/8/

//this app:
angular.module('myApp', ['autoActive']);

window.location.hash = '#/'; //All hash paths need to start with a /, it happens automaticaly with ngResource and the like...

//the module we are demonstrating:
(function () {
    angular.module('autoActive', [])
        .directive('autoActive', ['$location', function ($location) {
        return {
            restrict: 'A',
            scope: false,
            link: function (scope, element) {
                function setActive() {
                    var path = $location.path();
                    if (path) {
                        angular.forEach(element.find('li'), function (li) {
                            var anchor = li.querySelector('a');
                            if (anchor.href.match('#' + path + '(?=\\?|$)')) {
                                angular.element(li).addClass('active');
                            } else {
                                angular.element(li).removeClass('active');
                            }
                        });
                    }
                }

                setActive();

                scope.$on('$locationChangeSuccess', setActive);
            }
        }
    }]);
}());
li.active {
    background-color: red;
}
<div ng-app="myApp">
    <div auto-active>
        <ul>
            <li><a href="#/?some=data">This link points to #/fun1</a>
            </li>
            <li><a href="#/fun2?some=data">This link points to #/fun2</a>
            </li>
            <li><a href="#/fun3">This link points to #/fun3</a>
            </li>
            <li><a href="#/fun4">This link points to #/fun4</a>
            </li>
            <li><a href="#/fun5?some=data">This link points to #/fun5</a>
            </li>
        </ul>
    </div>
</div>