<body>
<button id="btnId">Click Me!</button>
</body>
答案 0 :(得分:1)
您可以通过两种方式绑定事件
1。 https://stackoverflow.com/a/51474887/4270123
2。。这是示例
class Button extends React.Component {
handleClick(e) {
e.preventDefault();
console.log(e.target.id);
alert("#" + e.target.id + " is clicked");
}
render() {
return (
<button onClick={ (e) => this.handleClick(e) } id="btnId">
Click Me!
</button>
);
}
}
答案 1 :(得分:0)
下面的代码可能会给您一个想法。
class Button extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleClick.bind(this);
}
handleClick(e) {
e.preventDefault();
alert("I am clicked");
}
render() {
return (
<button onClick={this.handleClick}>
Click Me!
</button>
);
}
}