我想在 React 中的 div 上使用 keyDown 事件。我愿意:
componentWillMount() {
document.addEventListener("keydown", this.onKeyPressed.bind(this));
}
componentWillUnmount() {
document.removeEventListener("keydown", this.onKeyPressed.bind(this));
}
onKeyPressed(e) {
console.log(e.keyCode);
}
render() {
let player = this.props.boards.dungeons[this.props.boards.currentBoard].player;
return (
<div
className="player"
style={{ position: "absolute" }}
onKeyDown={this.onKeyPressed} // not working
>
<div className="light-circle">
<div className="image-wrapper">
<img src={IMG_URL+player.img} />
</div>
</div>
</div>
)
}
它工作得很好,但我想用 React 风格做更多。我试过
onKeyDown={this.onKeyPressed}
在组件上。但它没有反应。我记得它适用于输入元素。
Codepen http://codepen.io/lafisrap/pen/OmyBYG
我该怎么做?
你应该使用tabIndex能够聆听的属性onKeyDown
React 中 div 上的事件。环境tabIndex="0"
应该解雇你的处理者。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)