focus
HTMLElement.focus() 상세
The HTMLElement.focus()
method sets focus on the specified element, if it can be focused. The focused element is the element which will receive keyboard and similar events by default.
focus 가능한 Class 파생들
There isn't a definite list, it's up to the browser. The only standard we have is DOM Level 2 HTML, according to which the only elements that have a focus()
method are HTMLInputElement
, HTMLSelectElement
, HTMLTextAreaElement
and HTMLAnchorElement
. This notably omits HTMLButtonElement
and HTMLAreaElement
.
Try to add the javascript code soon after the input element, so it will execute before the page load complete. In your case autofocus
attribute is set to the element but focusing the element which has autofocus
by browser is already done. so you setting the value after browser set the focus. when browser trying to set it no attribute is there. try like following code
<input type="text">
<script>
document.querySelector('input').setAttribute('autofocus', 'autofocus');
</script>
If you need to do it on button click or something, you need to do it in JavaScript. setting an attribute doesn't mean browser is going to execute it at anytime.
!!중요한건 DOM이 mount 되기도 전에 작성된 스크립트가 먼저 실행되기 때문에 input.focus()가 표현되지 않았던 것이다.
Author And Source
이 문제에 관하여(focus), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hqillz/focus저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)