2017-12-06 3 views
0

これは私のコードです。アドバイスをして、onclick関数の後にアイコンの色を変更したいと思います。jcleryを使ってonclick関数の後にicon色を塗りつぶす方法

<div class="timeline-footer"> 
    <a 
    id="changeicon" 
    onClick={(event) => this.handleCount(post.id, event)} 
    class="m-r-25" 
    ><i class="fa fa-heart-o"></i> {post.rating} Like</a> 
</div> 

これは私の評価カウント機能である

handleCount(id, event) { 
    event.classList.toggle("icon-heart"); 

    fetch(this.state.url + '/rating/' + id, { 
    method: 'POST', 
    header: { 
     'Accept': 'applicatoin/json', 
     'Content-Type': 'application/json' 
    } 
    }) 
    .then((response) => { 
    window.location.reload(); 
    }) 
} 
+0

これに合っている場合むしろ簡単です。クラスをstateプロパティにして、onClick関数の最後に状態を変更することができます。 – Gegenwind

+0

btw、反応では、 'class'の代わりに' className'を使います。[ReactJSのDOM要素](https://reactjs.org/docs/dom-elements.html) – 3Dos

答えて

2

はあなたが各クリック後の色を変更しますか? あなたはそれがより「反応ウェイ」を変更する状態を経jqueryのを使用せずに行うことができます。

changeColor = (color) => this.setState({iconColor: color}); 

getIconStyle =() => ({ 
    color: this.state.iconColor 
}); 

handleCount = (id, event) => { 
    event.classList.toggle("icon-heart"); 

    this.changeColor("red"); // or anyway you want 

    fetch(this.state.url + '/rating/' + id, { 
     method: 'POST', 
     header: { 
      'Accept': 'applicatoin/json', 
      'Content-Type': 'application/json' 
     } 
    }) 
    .then((response) => { 
     window.location.reload(); 
    }) 
} 

内部レンダリング:代わりにスタイルの

<div className="timeline-footer"> 
    <a 
     id="changeicon" 
     onClick={(event) => this.handleCount(post.id, event)} 
     style={this.getIconStyle()} 
     className="m-r-25" 
    > 
      <i className="fa fa-heart-o"></i> {post.rating} Like 
    </a> 
</div> 

または変更するクラスを、それはあなたのケース

+0

これでいいです。私の投稿は動的なデータです。すべての投稿のアイコンは赤に変わります。 –

+0

@GuGueを修正するには 'this.changeColor(" red ")'を 'this.changeColor(this.getColor (some_args)) ' – Rhymmor

関連する問題