2017-11-10 8 views
0

私はAngular CLIを使用し、メニューリストを持っています。私はTSにスタイルを変更するにはどうすればよいクリック時のブロックスタイルを変更する

export class ProfileComponent implements OnInit { 


    constructor() { } 

    changeColor() { 
    let menuButton = document.getElementById("menu_btn"); 
    } 

:私は

<ul> 
    <li class="menu_btn">1</li> 
    <li class="menu_btn">2</li> 
    <li class="menu_btn">3</li> 
    <li class="menu_btn">4</li> 
</ul> 

TSをクリックした後、李の背景色を変更したいのですが?または何らかの方法

+0

forget(click)= "changeColor()" – tatamingi

+0

'menu_btn'はクラスではないIDです。あなたはidで要素を取得しようとしています –

+1

https://stackoverflow.com/a/40754410/2894798 –

答えて

2
export class ProfileComponent implements OnInit { 


    constructor() { } 

    changeColor() { 
     let menuButton = document.getElementsByClassName("menu_btn") as HTMLCollectionOf<HTMLElement>; 
     menuButton.style.backgroundColor = 'yourColor'; 
    } 

これは何のIDあなたが好き使用することができます

使用クラス名の代わりに

​​

はありません、あなたのHTMLで

+1

あなたは 'getElementsByClass'を意味します –

+0

正解: – iamsankalp89

+0

'getElementsByClass'プロパティが 'Document'型に存在しません。 – tatamingi

0

をクリックしてのaferすべてmenu_btnクラスを変更しますこのスタイルを変更するには

menuButton.style.backgroundColor = 'red'; 
関連する問題