Reactjs 更改JSX中焦点的输入类型';行不通
我有一个文本字段,其中有占位符“Dates From”。我想做的是将焦点事件的输入框类型更改为日期类型。但是 但是下面提到的解决方案不适用于JSXReactjs 更改JSX中焦点的输入类型';行不通,reactjs,single-page-application,Reactjs,Single Page Application,我有一个文本字段,其中有占位符“Dates From”。我想做的是将焦点事件的输入框类型更改为日期类型。但是 但是下面提到的解决方案不适用于JSX <input placeholder="Date" type="text" onFocus="(this.type='date')" id="date"> 如何使这个东西在ReactJs上工作,或者如何实现相同的目标?使用匿名函数应该可以,使用e.target: <input placeholder="Date" type
<input placeholder="Date" type="text" onFocus="(this.type='date')" id="date">
如何使这个东西在ReactJs上工作,或者如何实现相同的目标?使用匿名函数应该可以,使用
e.target
:
<input placeholder="Date" type="text" onFocus={(e) => e.target.type = 'date'} id="date" />
e.target.type='date'}id=“date”/
您可以看到它的作用。也许您可以采取基于状态的方法,在执行以下操作时,更新组件的状态以跟踪输入字段的
类型:
onFocus={ () => this.setState({ typeOfFocused : 'date' }) }
执行此操作将触发重新渲染,从而导致输入元素的类型通过以下方式切换到date
:
render() {
// Extract typeOfFocused from state
const { typeOfFocused } = this.state
// Render input type with typeOfFocused if present, or as text by default
return (<input placeholder="Date"
type={ typeOfFocused || 'text' }
onFocus={ () => this.setState({ typeOfFocused : 'date' }) }
onBlur={ () => this.setState({ typeOfFocused : '' }) }
id="date">)
}
render(){
//从状态中提取聚焦的类型
const{typeOfFocused}=this.state
//如果存在,则使用typeOfFocused呈现输入类型,或者默认情况下作为文本呈现输入类型
return(this.setState({typeOfFocused:'date'})}
onBlur={()=>this.setState({typeOfFocused:'})}
id=“日期”>)
}
下面是试试这段代码
handleFocus(活动){
event.target.type='date';
}
render(){
报税表(
姓名:
);
}
Try this code
handleFocus(event) {
event.target.type = 'date';
}
render() {
return (
<label>
Name:
<input type="text" placeholder="please Enter date" onFocus={this.handleFocus.bind()} />
</label>
);
}