Unable to focus an input using JavaScript in IE11

前端 未结 3 1380
孤独总比滥情好
孤独总比滥情好 2021-01-12 14:41

I\'m unable to get IE11 to focus an input element after it is inserted into the DOM. The element won\'t receive text input after being focused, but its placeholder text is n

3条回答
  •  抹茶落季
    2021-01-12 15:12

    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. 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
    (Code: https://codepen.io/macjohnny-zh/pen/WPXWvy)

    Works in IE, too: https://codepen.io/macjohnny-zh/details/LqOvbZ
    (Code: https://codepen.io/macjohnny-zh/pen/LqOvbZ)

    Note: this also happens e.g. for

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

    See https://github.com/angular/material2/issues/15093

提交回复
热议问题