框架《React》库的学习笔记(从9到)

九. refs和事件处理

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>refs和事件处理</title>
</head>
<body>
    <script src="https://cdn.bootcss.com/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.bootcss.com/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.bootcss.com/babel-standalone/7.0.0-beta.3/babel.js"></script>
    <!--这个库是用于限定输入时的数据类型,-->
    <script src="https://cdn.bootcss.com/prop-types/15.6.1/prop-types.js"></script>

    <div id="example"></div>
    <script type="text/babel">
    /*
    需求:自定义组件,功能说明如下:
    1. 界面如页面所示
    2. 点击按钮,提示第一个输入框中的值
    3. 当第2个输入框失去焦点时,提示这个输入框中的值
    */

    //1. 定义组件
    class MyComponent extends React.Component {

        constructor (props) {
            super(props);

            this.showInput = this.showInput.bind(this);
            this.handleBlur = this.handleBlur.bind(this);
        }

        showInput () {
            const input = this.refs.content
            alert(this.input.value)
        }

        handleBlur (event) {
            alert(event.target.value)
        }
    

        render (){
            return (
                <div>
                    <input type="text" ref="content"/>&nbsp;&nbsp;
                    <input type="text" ref= {input => this.input = input}  /> &nbsp;&nbsp;
                    <button onClick={this.showInput}>提示输入</button> &nbsp;&nbsp;
                    <input type="text" placeholder="失去焦点提示内容" onBlur= {this.handleBlur}/>
                </div>
            )
        }
    }

    //2. 渲染组件标签
    ReactDOM.render(<MyComponent/>, document.getElementById('example'))

</script>
</body>
</html>

十. 组件组合使用_初始化显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值