将输入元素插入 DOM 后,我无法让 IE11 将其聚焦。元素在获得焦点后不会接收文本输入,但其占位符文本不再可见。该元素由 React 创建,我通过 React 中的refs
对象访问它componentDidMount
:
componentDidMount() {
this.refs.input.getDOMNode().focus();
}
我尝试使用setTimeout
以下方法添加短暂延迟:
componentDidMount() {
setTimeout(() => this.refs.input.getDOMNode().focus(), 10);
}
我也曾尝试加入tabIndex
的"1"
到输入。
如果有帮助,这里是渲染的 JSX:
<div style={style}>
<div style={labelStyle}>Enter a name to begin.</div>
<form onSubmit={this.submit} style={formStyle}>
<input
tabIndex="1"
ref="input"
value={this.state.username}
onChange={this.updateUsername}
placeholder="New User Name"
style={inputStyle}
/>
<button {...this.getBrowserStateEvents()} style={this.buildStyles(buttonStyle)}>Create</button>
</form>
</div>
是否有一些技巧可以让我在 IE11 中专注于工作,而我不知道?