我想基于活跃的tabswitch
和tab1
来更改类tab2
的背景颜色。我正在Angular 6上执行此代码。
<div class="tabswitch">
<input type="radio" id="tab1" name="tabs" checked>
<label for="tab1">tab1</label>
<input type="radio" id="tab2" name="tabs">
<label for="tab2">tab2</label>
<div id="content1">b</div>
<div id="content2">a</div>
</div>
答案 0 :(得分:0)
To use my logic, you should declare a variable named activeTab
in your component and keep activated tab name inside of that variable and then you can use [ngStyle]
directive to achieve this,
<div class="tabswitch" [ngStyle]="{'background-color': activeTab === 'tab1' ? 'green' : 'red' }">
<input type="radio" id="tab1" name="tabs" checked>
<label for="tab1">tab1</label>
<input type="radio" id="tab2" name="tabs">
<label for="tab2">tab2</label>
<div id="content1">b</div>
<div id="content2">a</div>
</div>
More examples are here.
答案 1 :(得分:-1)
<div class="tabswitch" ng-class="active">
<input type="radio" id="tab1" name="tabs" ng-click="active='tab1c'" checked>
<label for="tab1">tab1</label>
<input type="radio" id="tab2" name="tabs" ng-click="active='tab2c'">
<label for="tab2">tab2</label>
<div id="content1">b</div>
<div id="content2">a</div>
</div>
为类tab1c和tab2c创建CSS