脚本宝典收集整理的这篇文章主要介绍了事件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
import React, {component, PRopTyPEs} From 'react';
import ReactDOM from 'react-dom';
class People extends Component {
constructor(props){
super(props);
this.state = {
name: 'sisi',
school: 'hust',
age: '23'
}
}
handleOnfocus = (ev,varible) => {
console.log(ev.target.placeholder);
console.log(varible)
}
render(){
return <div>
名字:<input type='text' onFocus={this.handleOnfocus()} placeholder='name'/><br />
学校:<input type='text' placeholder='school'/><br />
年纪:<input type='text' placeholder='age'/>
</div>
}
}
ReactDOM.render(
<People name={'jianwen'} />,
document.getElementById('example')
)
上面代码中,onFocus={this.handleOnfocus('haha')}
,是表示立即执行this.handleOnfocus
函数,即使没有触发onFocus
,因为只有这样写onFocus={this.handleOnfocus}
才表示触发focus事件就执行。 为了可以传递参数,我们修改handleOnfocus函数为下面这样:
handleOnfocus = (variable) => {
return (ev) => {
console.log(ev.target.placeholder);
console.log(varible)
}
}
并且不能写成下面的形式
handleOnfocus = (variable) => {
return (ev,variable) => {
console.log(ev.target.placeholder);
console.log(varible)
}
}
以上是脚本宝典为你收集整理的事件全部内容,希望文章能够帮你解决事件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。