h5 input react ref

本文探讨了在H5和React应用中,如何使用ref属性来获取和操作input元素,详细解释了ref的概念及其在处理用户输入时的重要性。
摘要由CSDN通过智能技术生成

h5 input 

文本:   <input type="text" name="username" value="账号" /><br />
密码:   <input type="password" name="password" /><br />
邮箱:   <input type="email" name="email" /><br />
搜索:   <input type="search" name="search" /><br />
URL:  <input type="url" name="url" /><br />
年月日: <input type="date" name="date" value="2016-06-13" /><br />
年月:   <input type="month" name="month" value="2016-06" /><br />
星期:  <input type="week" name="week" value="2016-W23" /><br />
时间:  <input type="time" name="time" value="15:00" /><br />
datetime-local:<input type="datetime-local" name="local" value="2016-06-18 15:02" /><br />
数字:  <input type="number" name="number" /><br />
color: <input type="color" name="color" /><br />
range滑块:<input type="range" value="100" />

react ref

 //当我们在DOM Element中使用ref时,回调函数将接收当前的DOM元素作为参数
    class Input extends React.Component {
        constructor(props){
            super(props);
        }
        componentDidMount(){
            this.textInput.focus(); // 自动获取焦点
        }
        render(){
            return (
                <div>
                    {this.props.name} <input ref={(value)=>{this.textInput =value}} type={this.props.type}></input>
                </div>
            )
        }
    }
    class Form extends React.Component {
        constructor(props){
            super(props);
        }
        render(){
            return (
                <div>
                    <Input name ={"username"} type={"text"}/>
                    <Input name ={"password"} type={"password"}/>
                    <Input name ={"email"} type={"email"}/>
                    <Input name ={"search"} type={"search"}/>
                    <Input name ={"url"} type={"url"}/>
                    <Input name ={"date"} type={"date"}/>
                    <Input name ={"month"} type={"month"}/>
                    <Input name ={"week"} type={"week"}/>
                    <Input name ={"time"} type={"time"}/>
                    <Input name ={"datetime-local"} type={"datetime-local"}/>
                    <Input name ={"number"} type={"number"}/>
                    <Input name ={"color"} type={"color"}/>
                    <Input name ={"range"} type={"range"}/>
                </div>
            )
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值