当前位置:网站首页>今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
2022-07-17 00:06:00 【丁丁丁dingdingding】
事件操作
在虚拟dom中调用事件,事件名首字母要大写,例如:onClick,在调用的函数需要改变this指向,或者定义成箭头函数
class Header extends React.Component{//子
fun(){
this.props.foo("asd")
}
render(){
return (<div><h1 onClick={this.fun}>点击我</h1></div>)
}
}
组件变量state
class App extends React.Component{
constructor(props){
super(props)
//创建变量
this.state={msg:"hello"}
}
//渲染页面
render(){//组件下的变量调用
return (<h1>{this.state.msg}</h1>)
}
}
ReactDOM.render(
(<div>
<App />
</div>),
document.getElementById("app")
)
refs属性
refs是react下的一个特殊属性,可以在虚拟dom里绑定一个属性值,在函数下可以直接操作虚拟dom标签和组件,这样就可以操作标签的样式,文本,属性等内容,让获取标签更简单
inpvalue(event){
this.refs.inp.style.backgroundColor="pink"
this.refs.inp.vallue="好"
}
<input type="text" onChange={this.inpvalue} value={this.state.msg} ref="inp"/>
练习:做一个登录界面,页面上有用户名和密码框
点击按钮,用react验证用户名和密码是否为空,并在页面上给出提示
<body>
<div id="app"></div>
<script type="text/babel">
class App extends React.Component{
constructor(props){
super(props)
this.state={user:"",pwd:"",up:""}
this.fun1=this.fun1.bind(this)
this.fun2=this.fun2.bind(this)
this.value=this.value.bind(this)
}
fun1(e){
var v=e.target.value
this.setState({user:v})
}
fun2(e){
var v=e.target.value
this.setState({pwd:v})
}
value(){
var a=this.state.user
var b=this.state.pwd
if(a=="" || b==""){
this.setState({up:"用户名或密码不能为空"})
}
else{
this.setState({up:"登录成功"})
}
}
render(){
return (
<div>
<p>用户名:<input type="text" onChange={this.fun1} value={this.state.user}/></p>
<p>密码:<input type="password" onChange={this.fun2} value={this.state.pwd}/></p>
<p><input type="button" value="点击" onClick={this.value}/></p>
{this.state.up}
</div>
)
}
}
ReactDOM.render(
(<div>
<App />
</div>),
document.getElementById("app")
)
</script>
</body>边栏推荐
- uni canvas截取图片
- The C Programming Language (2nd)--笔记--1.7
- Single page application spa and multi page application MPa
- Replace special characters in URL (%e2%80%8b)
- Summary of XML external entity injection (xxE target recurrence)
- “我的”Bug大全(转载)
- Registry hijacking triggers malicious programs
- Gateway Kong route adding instructions
- The C Programming Language(2nd)--笔记--1.10
- Common mode attack and shared prime of RSA
猜你喜欢

gtest与gmock的安装与使用

Oracle database startup and shutdown steps

深度之眼三——(3)】 数学:矩阵特征值与特征向量1

Codeforces round #664C

How to modify the color of a line of text in the elemtnui table (elemtnui table modifies the color of a line of text)

服务器如何安装宝塔面板?(宝塔面板安装教程)

量化行业知识汇总

Assemblage stylisé de cartes de commutation auto - encapsulées

everything搜索不到startup_lpc11x.s文件

Cve-2022-34265 Django extract & TRUNC SQL injection vulnerability recurrence
随机推荐
Oracle database parameter change
深度之眼三——(6)】数学:矩阵对角化及二次型1
(七)流程控制
服务器如何安装宝塔面板?(宝塔面板安装教程)
C Programming Language (2nd Edition)--读书笔记--1.5.3
智能指针(shared_ptr、unique_ptr、weak_ptr)
JS string to object JS object to string JS string to object
How to modify the color of a line of text in the elemtnui table (elemtnui table modifies the color of a line of text)
The C Programming Language (2nd)--笔记--1.6
Day10 front back continuous adjustment
自己封装的风格化的开关卡片组件
C Programming Language (2nd Edition)--读书笔记--1.5.4
网站如何统计访问人数?51LA如何安装使用?
列表懒加载和图片懒加载
当 std::bind 遇上 this
[SWPU 2019] network TTL encryption and some related knowledge
Raw Socket抓包,某些端口的数据包抓不到
The C Programming Language(2nd)--笔记--1.9
Vue project deployment, cleaning cache
uni 阻止按钮多次点击 按钮多次点击