Reactjs 单击时更改按钮的样式

Reactjs 单击时更改按钮的样式,reactjs,react-bootstrap,Reactjs,React Bootstrap,是否可以更改我的按钮的onClick功能的背景色 例如,单击背景色:黑色,再单击背景色:白色 我尝试过类似于这种风格的东西,但没有结果 我已经设法使覆盖工作,并在其中插入所需的数据。 但是没有找到任何能帮助我的帖子。 我正在使用react引导。 这是我的密码 const metaDataOverlay = ( <div> <style type="text/css">{` .btn-overlay { background-color: white

是否可以更改我的按钮的
onClick
功能的
背景色

例如,单击背景色:黑色,再单击背景色:白色

我尝试过类似于
这种风格的东西,但没有结果

我已经设法使覆盖工作,并在其中插入所需的数据。 但是没有找到任何能帮助我的帖子。 我正在使用react引导。 这是我的密码

  const metaDataOverlay = (
  <div>
  <style type="text/css">{`
  .btn-overlay {
    background-color: white;
    margin-right: -15px;
    margin-left: -15px;
    padding-bottom: -20px;
    padding: 0;
  }
  `}</style>

      <ButtonToolbar>
        <ButtonGroup>
        <OverlayTrigger trigger={['hover', 'focus']} placement="left" overlay={popoverHoverFocus}>
          <Button bsStyle="overlay" onClick={ clicked } onKeyPress={ keypress }>
            <div className={bemBlocks.item().mix(bemBlocks.container("item"))} data-qa="hit">
              <a href={url} onClick={(e)=>{e.preventDefault(); console.log("123")}}>
                <div>
                  <img data-qa="poster" className={bemBlocks.item("poster")} src={result._source.poster} width="240" height="240"/>
                </div>
              </a>
            </div>
          </Button>
        </OverlayTrigger>
        </ButtonGroup>
      </ButtonToolbar>

    </div>
  )
const metaDataOverlay=(
{`
.btn覆盖{
背景色:白色;
右边距:-15px;
左边距:-15px;
垫底:-20px;
填充:0;
}
`}
)

您还可以访问事件和事件的当前目标

handleClick = (event) => {
   // accessible
   event.target.style
   event.target.classList //to change style via css
}

您可以尝试使用状态来存储颜色。也许这会让你知道如何解决这个问题:

class Test extends React.Component {
    constructor(){
        super();

        this.state = {
           black: true
        }
    }

    changeColor(){
       this.setState({black: !this.state.black})
    }

    render(){
        let btn_class = this.state.black ? "blackButton" : "whiteButton";

        return (
             <button className={btn_class} onClick={this.changeColor.bind(this)}>
                  Button
             </button>
        )
    }
}

React.render(<Test />, document.getElementById('container'));
类测试扩展了React.Component{
构造函数(){
超级();
此.state={
布莱克:没错
}
}
changeColor(){
this.setState({black:!this.state.black})
}
render(){
让btn_class=this.state.black?“黑按钮”:“白按钮”;
返回(
按钮
)
}
}
React.render(,document.getElementById('container');

这是您访问的方式

    handleClick=(e)=>{
        console.log("this is working fine");
        e.preventDefault();
        e.target.style.color = 'black'
        console.log(e.target);
    }

如果您想要更动态地初始化状态,可以使用style afterwords函数的一些默认值来更新您的状态

changeStyles = () => {
    let element = document.getElementById('button')
    ReactDOM.findDOMNode(element).style.backgroundColor = this.state.isClicked?'black' : 'white'
}

通过这种方式,您可以只更改所需的样式属性,以防止CSS中出现重复的样式属性。

将其添加到工具提示中

<Tooltip cursor={{ fill: 'transparent' }} />

这里是另一个解决方案:

import React, { Component } from 'react';
import './App.css';

class App extends Component {
  constructor(props) {
      super(props);
      this.state = {
      BackgroundColor: "BLACK"};
  };

render(){
    return (
      <div className='app'>
        <button className={this.state.BackgroundColor === "BLACK" ? "Black" : "nothing"}
        onClick={() => {this.setState({BackgroundColor: "WHITE"})}}>CHANGE TO BLACK</button>
        <button className={this.state.BackgroundColor === "WHITE" ? "White" : "nothing"}
        onClick={() => {this.setState({BackgroundColor: "BLACK"})}}>CHANGE TO WHITE</button>
      </div>
    );
  }
}

export default App;

是的,它确实有帮助,我也发现这很有帮助,因为它有一个例子,让你用黑白的
event.target.style.backgroundColor=“red”
e.target.classList.add(“mystyle”)
render(){
    return (
      <div className='app'>
        <button className={this.state.BackgroundColor === "BLACK" ? "Black" : "White"}
        onClick={() => {this.setState({BackgroundColor: "WHITE"})}}>CHANGE TO BLACK</button>
      </div>
    );
  }
}
*{
  margin: 0;
  padding: 0;
 }

.app{
  display: flex;
  justify-content: center;
  align-items: center;
}
.Black{
  background-color: black;
  color: white;
}

.White{
  background-color: white;
  color: black;
}

.nothing{
  display: none;
}