简体   繁体   English

我如何在React JS中使用jQuery在onclick函数后填充图标颜色

[英]How can I fill icon color after onclick function using jquery in react js

Here is my code.Let me advice ,I want to change icon color after onclick function . 这是我的代码。请教我,我想在onc​​lick函数后更改图标颜色。

<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>

This is my rating count function 这是我的评分计数功能

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();
  })
}

Do you want to change color after each click? 每次单击后是否要更改颜色? You can do it more 'react-way' without using jquery through state changing: 您可以通过状态更改而不使用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();
    })
}

Inside render: 内部渲染:

<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>

Or change class instead of style if it suits your case 或者如果适合您的情况,请更改类别而不是样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应 js 中正确使用 Onclick 和 function? - How can I use Onclick propertly with function in react js? 如何使用 Action/Flux [JS/REACT] 处理 onClick - How can I handle a onClick using Action/Flux [JS/REACT] 如何在 React 中使用 onClick() 函数渲染不同的组件? - How can I render different component using onClick() function in React? 我在 JS 中使用 function 将颜色更改为红色,然后再更改为绿色 onclick - I'm using a function in JS that changes the color to red and then to green onclick 如何在使用 jquery 加载页面后更改图标的颜色 - how to change the color of an icon after the page loads using jquery 仅在JS验证后如何执行Jquery onclick =“”函数 - How to execute Jquery onclick=“” function only after JS validation 如何将表单中的变量传递给 React.js 中的 onclick 函数? - How can I pass a variable in a form to to a onclick function in React.js? 我如何在 React JS 中的 function 中使用我的 function - How can i using my function inside function in React JS 如何将 onClick 功能添加到 React Js 中的特定列? - How can I Add onClick functionality to particular column in React Js? 使用JQuery执行onclick函数时,如何获得正确的图像src? - How can I get the right image src when I do an onclick function using JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM