加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React无法专注于div

发布时间:2020-12-15 20:45:36 所属栏目:百科 来源:网络整理
导读:当我点击像 here这样的按钮时,我试着把注意力集中在一个div上.我将我的问题减少到下面的代码,虽然在div对象上调用焦点,但是永远不会调用onFocus.此外,autoFocus无法解决我的问题,因为我希望能够在组件安装后多次更改焦点. class App extends React.Component
当我点击像 here这样的按钮时,我试着把注意力集中在一个div上.我将我的问题减少到下面的代码,虽然在div对象上调用焦点,但是永远不会调用onFocus.此外,autoFocus无法解决我的问题,因为我希望能够在组件安装后多次更改焦点.
class App extends React.Component {
    constructor() {
        super();
        this.my_refs = {};
        this.focusByID.bind(this);
    }

    focusByID(id){
        let myRef = this.my_refs[id];
        if(myRef){
            console.log('focusing on ',id,myRef);
            myRef.focus();
        }
    }
    render() {
        return (
            <div>
                <div
                    id="bigRedDiv"
                    style={{height : 200,width : 200,backgroundColor : 'red'}}
                    ref={(input) => this.my_refs['bigRedDiv'] = input }
                    onFocus={() => console.log('FOCUS IS ON BIG RED DIV')}
                    onClick={() => this.focusByID('bigRedDiv')}
                >
                </div>
            </div>
        );
    }
}

这是包含此代码的fiddle.

只是:
...
<div 
    tabIndex="0" //use this attribute
    id="bigRedDiv" 
    style={{height : 200,backgroundColor : 'red'}} 
    ref={(input)=> this.my_refs['bigRedDiv'] = input } 
    onFocus={() => console.log('FOCUS IS ON BIG RED DIV')} 
    onClick={() => this.focusByID('bigRedDiv')} >
</div>
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读