简体   繁体   English

无法在IE11中使用JavaScript聚焦输入

[英]Unable to focus an input using JavaScript in IE11

I'm unable to get IE11 to focus an input element after it is inserted into the DOM. 在将输入元素插入DOM后,我无法让IE11聚焦输入元素。 The element won't receive text input after being focused, but its placeholder text is no longer visible. 聚焦后元素不会接收文本输入,但其占位符文本不再可见。 The element is created by React, and I'm accessing it through React's refs object in componentDidMount : 该元素由React创建,我通过componentDidMount React的refs对象访问它:

componentDidMount() {
    this.refs.input.getDOMNode().focus();
}

I have tried adding a short delay using setTimeout : 我尝试使用setTimeout添加一个短暂的延迟:

componentDidMount() {
    setTimeout(() => this.refs.input.getDOMNode().focus(), 10);
}

I have also tried adding a tabIndex of "1" to the input. 我也尝试在输入中添加一个tabIndex "1"

In case it's helpful, here is the rendered JSX: 如果它有用,这里是渲染的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>

Is there some trick to getting focus to work in IE11 that I'm unaware of? 是否有一些技巧可以让我专注于在IE11中工作而我不知道?

The issue was focusing in IE11 is broken when the css property -ms-user-select: none is applied to the input. 当css属性-ms-user-select: none应用于输入时,问题集中在IE11中。 So by changing: 所以通过改变:

* {
  -ms-user-select: none;
}

into

*:not(input) {
  -ms-user-select: none;
}

I was able to solve the problem. 我能够解决问题。 Here is a codepen for reproducing the issue: http://codepen.io/anon/pen/yNrJZz 这是一个用于复制问题的codepen: http ://codepen.io/anon/pen/yNrJZz

I don't think that your issue is coming from the focus() function, I think it's coming from the selector. 我不认为你的问题来自focus()函数,我认为它来自选择器。

To prove it I just opened my IE11 and tried to focus the SO search input on the top right of the page using the following command: 为了证明这一点,我刚刚打开IE11并尝试使用以下命令将SO搜索输入集中在页面的右上角:

document.getElementById('search')[0].focus()

So, just open your IE console (F12) and type that, it should focus the search input. 所以,只需打开你的IE控制台(F12)并键入它,它应该集中搜索输入。 If so, then the issue is coming from the selector which isn't an input as you may think. 如果是这样,那么问题就来自选择器,它不是您想象的输入。

It works on my IE 11.0.9600.17905 它适用于我的IE 11.0.9600.17905

As stated in https://stackoverflow.com/a/31971940 , running element.focus() has no effect in IE11 when the css property -ms-user-select: none is set. https://stackoverflow.com/a/31971940中所述 ,当css属性-ms-user-select: none设置时,运行element.focus()在IE11中无效。 A workaround can be 解决方法可以是

element.style['-ms-user-select'] = 'text';
element.focus()
element.style['-ms-user-select'] = '';

Does not work in IE: https://codepen.io/macjohnny-zh/details/WPXWvy 在IE中不起作用: https//codepen.io/macjohnny-zh/details/WPXWvy
(Code: https://codepen.io/macjohnny-zh/pen/WPXWvy ) (代码: https//codepen.io/macjohnny-zh/pen/WPXWvy

Works in IE, too: https://codepen.io/macjohnny-zh/details/LqOvbZ 在IE中也可以使用: https//codepen.io/macjohnny-zh/details/LqOvbZ
(Code: https://codepen.io/macjohnny-zh/pen/LqOvbZ ) (代码: https//codepen.io/macjohnny-zh/pen/LqOvbZ

Note: this also happens eg for 注意:这也发生在例如

:-ms-input-placeholder {
  -ms-user-select: none;
}

See https://github.com/angular/material2/issues/15093 请参阅https://github.com/angular/material2/issues/15093

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM