小编Dr.*_*man的帖子

使用event.target.id从bind获取id时,意外使用'event'no-restricted-globals(this)

此代码适用于Codepen:请参阅https://codepen.io/pkshreeman/pen/YQNPKB?editors=0010 但是我试图在我自己的'create-react-app'中使用它,并且错误为'不受限制 -全局'被触发event.target.id.什么是解决方法.除了使用事件目标之外,你如何从'this'得到id?

const Elem = (props) =>{ 
  return (<div>
    <h1 onClick={props.clickon} id="GM"> Good Morning! 
      <br/> 
      {props.name} {props.last} 
      <br />
      This is phase three</h1>
    <button id="btn1" onClick={props.clickon}> {props.text} </button>
      <button id="btn2" onClick={props.clickon}> Second Button </button>
      </div>
  );
};



class App extends React.Component{
  constructor(props) {
   super(props);
   this.handleClick = this.handleClick.bind(this);
 }

handleClick(){  
  var clickedId = event.target.id;
    console.log(clickedId);
  alert("It works! You clicked " + clickedId)
}
  render(){
    return (
    <Elem name = 'paul' last='shreeman' clickon={this.handleClick} text='PushMe'/>
  )
}
}

ReactDOM.render( …
Run Code Online (Sandbox Code Playgroud)

javascript this reactjs

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

javascript ×1

reactjs ×1

this ×1