TIL

23/10/20 TIL __ .focus()

GABOJOK 2023. 10. 20. 17:46

 

 

 

๐Ÿš—  input ์ปค์„œ ์ž๋™ ์œ„์น˜ ๊ธฐ๋Šฅ

 

 

์›ํ–ˆ๋˜ ๋ฐฉํ–ฅ

   ๐Ÿ‘‰ ํŽ˜์ด์ง€ ๋ฆฌ๋กœ๋“œ ๋˜๋ฉด ์ปค์„œ๊ฐ€ ์ธํ’‹์œผ๋กœ ์ž๋™ ์œ„์น˜๋˜๋„๋ก ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค. 

 

 

๊ตฌํ˜„ ๋ฐฉ๋ฒ•

 

  1. input ํƒœ๊ทธ์˜ ์†์„ฑ์œผ๋กœ autofocus ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.
<!-- ํŽ˜์ด์ง€ ๋กœ๋“œ๋˜์ž๋งˆ์ž ์ปค์„œ ์˜ค๋„๋ก autofocus์„ค์ • -->
<input id="searchInput" type="text" autofocus />

 

      ๐Ÿ‘‰  autofocus ์˜ ํŠน์ง•์€ ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ ๋˜์ž๋งˆ์ž ์ž๋™์œผ๋กœ ํฌ์ปค์‹ฑ์ด ๋œ๋‹ค. 

 

 

 

   2.  .focus() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค. 

//input ์ž๋™ ์ปค์„œ
let input = document.querySelector("#searchInput");
input.focus();

 

.focus() ๋Š” ์ฒ˜์Œ๋ณธ ๋ฉ”์†Œ๋“œ๋ผ ์ฐพ์•„๋ดค๋Š”๋ฐ, ์ฐธ๊ณ ํ•œ ๋งํฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 

 

 

๐Ÿง    . focus( ) 

  • ํŠน์ •ํ•œ ํฌ์ปค์Šค๋กœ ์ด๋™ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค. 
  • ํฌ์ปค์Šค๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๋ฉด . blur( ) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

https://ko.javascript.info/focus-blur

 

focus์™€ blur

 

ko.javascript.info

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus

 

HTMLElement: focus() method - Web APIs | MDN

The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default.

developer.mozilla.org