Language/JavaScript (WEB)

๐ŸŒ HTML ํฌ์ปค์Šค ์ œ์–ดํ•˜๊ธฐ - focus, blur

์ธํŒŒ_ 2021. 9. 22. 08:02

focus

HTML ํฌ์ปค์Šค ์ œ์–ด

์‚ฌ์ง„์ด๋‚˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ์ž‘์—…์„ ๋‹ค๋ค„๋ณด์‹  ๋…์ž ๋ถ„์€ ์•„๋งˆ focus์™€ blur ์ฒ˜๋ฆฌ์— ๋Œ€ํ•ด ๋“ค์–ด๋ณธ ์ ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์‚ฌ์ง„์— ๋ธ”๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ํ๋ ค์ง€๊ณ  ํฌ์ปค์Šค ์ฒ˜๋ฆฌํ•˜๋ฉด ํ๋ฆฐ๊ฒŒ ์›๋ž˜๋Œ€๋กœ ๋˜๋Œ์•„์˜จ๋‹ค.

HTML ํฌ์ปค์Šค๋Š” ์ด๋Ÿฌํ•œ ๊ฐœ๋…์„ ์—˜๋ฆฌ๋จผํŠธ์— ๊ฐ€์ ธ์˜จ ๊ฒƒ์œผ๋กœ ๋ณด๋ฉด ๋œ๋‹ค. HTML์—์„œ <button>, <input>, <select>, <a> ์™€ ๊ฐ™์€ ์—˜๋ฆฌ๋จผํŠธ์— focus ๋ฅผ ๋ถ€์—ฌํ•˜๋ฉด ํ•ด๋‹น ์š”์†Œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค๋ฅผ ์˜๋ฏธํ•œ๋‹ค. blur๋Š” ๋ฐ˜๋Œ€๋กœ ํฌ์ปค์Šค๋ฅผ ์žƒ์€ ์ˆœ๊ฐ„์„ ์ด์•ผ๊ธฐ ํ•˜๋Š”๋ฐ, ์ž…๋ ฅ๋ž€์—์„œ ๋”์ด์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

 

focus / blur ์ด๋ฒคํŠธ

focus ์ด๋ฒคํŠธ๋Š” ์š”์†Œ๊ฐ€ ํฌ์ปค์Šค๋ฅผ ๋ฐ›์„ ๋•Œ, blur ์ด๋ฒคํŠธ๋Š” ํฌ์ปค์Šค๋ฅผ ์žƒ์„ ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.

  • onfocus : ์ž…๋ ฅ์ฐฝ์ด ํฌ์ปค์Šค ๋ฐ›์„ ๊ฒฝ์šฐ ์ž‘๋™
  • onblur : ์ž…๋ ฅ์ฐฝ ํฌ์ปค์Šค๋ฅผ ์žƒ์œผ๋ฉด ์ž‘๋™
// ์š”์†Œ๊ฐ€ ํฌ์ปค๋“œ ๋˜๋ฉด ์ด๋ฒคํŠธ ๋ฐœ์ƒ
document.getElementById("fname").addEventListener("focus", () => {
  document.getElementById("fname").style.backgroundColor = "red";
});

// ์š”์†Œ๊ฐ€ ๋ธ”๋Ÿฌ ๋˜๋ฉด ์ด๋ฒคํŠธ ๋ฐœ์ƒ
document.getElementById("fname").addEventListener("blur", () => {
  alert("Input field lost focus.");
  document.getElementById("fname").style.backgroundColor = "white";
});

See the Pen focus, blur - 1 by barzz12 (@inpaSkyrim) on CodePen.

๊ทธ๋Ÿฐ๋ฐ ์ผ๋ฐ˜ focus์™€ blur ์ด๋ฒคํŠธ๋Š” ๋ฒ„๋ธ”๋ง ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งŒ์ผ ์ด๋ฒคํŠธ ์œ„์ž„ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์บก์ฒ˜๋ง(capture)์„ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ๋ณ„๋„์˜ ํฌ์ปค์Šค ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

 

focusin / focusout ์ด๋ฒคํŠธ

focusin๊ณผ focusout ์ด๋ฒคํŠธ๋Š” focus, blur์™€ ๊ธฐ๋Šฅ์€ ๋น„์Šทํ•˜์ง€๋งŒ ๋ฒ„๋ธ”๋ง์ด ๋œ๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. 

focusin-focusout

๋ณด๋‹ค ๋ช…ํ™•ํžˆ ํ•˜์ž๋ฉด ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์š”์†Œ์— ํฌ์ปค์Šค๋ฅผ ์ค„ ๊ฒฝ์šฐ focusin ๋ณด๋‹ค focus ์ด๋ฒคํŠธ๊ฐ€ ๋จผ์ € ๋ฐœ์ƒ๋œ๋‹ค. ๋‹ค๋งŒ ์ด ๋‘ ์ด๋ฒคํŠธ๋Š” ์„œ๋กœ ๋งค์šฐ ๊ฐ€๊น๊ฒŒ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋™์‹œ์— ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ด๋„ ์ƒ๊ด€์—†๋‹ค.

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, input ์š”์†Œ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜์–ด form ํƒœ๊ทธ์˜ focusin, focusout ์ด๋ฒคํŠธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ focusinHandler() ๊ฐ€ ๋ฐœ์ƒ๋จ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

focusin-focusoutfocusin-focusout

<form onfocusin="focusinHandler(event)" onfocusout="focusoutHandler(event)">
  User Name <input type="text" name="username" />
  <br><br> 
  Email <input type="text" name="email" />
</form>

See the Pen focus, blur - 2 by barzz12 (@inpaSkyrim) on CodePen.

 

focus / blur ๋ฉ”์„œ๋“œ

์‚ฌ์šฉ์ž์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ํ†ตํ•ด ํฌ์ปค์Šค ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฉ”์„œ๋“œ๋กœ ์ผ๋ถ€๋Ÿฌ ํฌ์ปค์‹ฑ์„ ํ•˜๋„๋ก ์กฐ์ž‘ํ• ์ˆ˜๋„ ์žˆ๋‹ค.

  • focus() : ํ•ด๋‹น ์š”์†Œ์— ํฌ์ปค์Šค๋ฅผ ๋ถ€์—ฌ
  • blur() : ํ•ด๋‹น ์š”์†Œ์— ํฌ์ปค์Šค๋ฅผ ์žƒ์Œ

๋‹ค์Œ ์˜ˆ์ œ๋Š” ์—”ํ„ฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด focus() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜์–ด ๋ฐ”๋กœ ๋‹ค์Œ ์ž…๋ ฅ์ฐฝ ์š”์†Œ๋กœ ํฌ์ปค์‹ฑํ•˜์—ฌ์„œ ์—ฐ๋‹ฌ์•„ ๋ฐ”๋กœ๋ฐ”๋กœ ์ž…๋ ฅํ• ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์‘์šฉ๋ฒ•์ด๋‹ค.

<FORM>
  [๊ฐœ์ธ์ •๋ณด ์ž…๋ ฅ]<br>
  ์ด๋ฆ„ : <INPUT id=pname type=text size=10 onkeydown='moveFocus("age")'><br>
  ๋‚˜์ด : <INPUT id=age type=text size=10 onkeydown='moveFocus("sex")'><br>
  ์„ฑ๋ณ„ : <INPUT id=sex type=text size=10 onkeydown='moveFocus("confirm")'>
</FORM>

<script>
    function moveFocus(next) {
      if (event.keyCode == 13) { //์—”ํ„ฐ๋ˆ„๋ฅผ๊ฒฝ์šฐ
        document.getElementById(next).focus();//์•„๊ทœ๋จผํŠธid ๋…ธ๋“œ๋กœ ์ด๋™ํ•ด์„œ focusํ•œ๋‹ค
      }
    }
</script>

See the Pen focus, blur - 0 by barzz12 (@inpaSkyrim) on CodePen.

 

ํ˜„์žฌ ํฌ์ปค์Šค๋œ ์š”์†Œ ํ™•์ธ

ํ˜„์žฌ ํฌ์ปค์Šค๋œ ์š”์†Œ๋Š” document.activeElement๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

<textarea id='textarea1'>textarea1</textarea>
<textarea id='textarea2'>textarea2</textarea>

<script>
    document.addEventListener("mouseup", (showSelected) => {
      alert(`${document.activeElement.id} ํด๋ฆญ !!`);
    });
</script>

See the Pen focus, blur - 5 by barzz12 (@inpaSkyrim) on CodePen.

 

์ž๋™ ํฌ์ปค์Šค

autofocus๋ผ๋Š” HTML ์†์„ฑ์„ ์š”์†Œ์— ๋ถ€์—ฌํ•˜๋ฉด ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋œ ํ›„ ์ž๋™์œผ๋กœ ํฌ์ปค์‹ฑ ๋œ๋‹ค.

<form action="/examples/media/action_target.php" method="get">
    ์ด๋ฆ„ : <input type="text" name="st_name"><br>
    ํ•™๋ฒˆ : <input type="text" name="st_id" autofocus><br> <!-- ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ์ž๋™์œผ๋กœ ํฌ์ปค์Šค -->
    ํ•™๊ณผ : <input type="text" name="department"><br>
    <input type="submit">
</form>

๋ชจ๋“  HTML ์š”์†Œ ํฌ์ปค์Šค ์ง€์›

 <button><input><select><a> ์™€ ๊ฐ™์€ ํŠน๋ณ„ํ•œ ์š”์†Œ ์™ธ์˜ ๋Œ€๋ถ€๋ถ„์˜ ์š”์†Œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌ์ปค์Šค๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ๋งŒ์ผ <div> ๋‚˜ <span> ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ์š”์†Œ์—๋„ ํฌ์ปค์Šค ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ์‹ถ๋‹ค๋ฉด tabindex ์†์„ฑ์„ ๋ถ€์—ฌํ•˜๋ฉด ๋œ๋‹ค.

 

tabindex ์†์„ฑ

tabindex ์†์„ฑ์ด ์žˆ๋Š” ์š”์†Œ๋Š” ์ข…๋ฅ˜์™€ ์ƒ๊ด€์—†์ด ํฌ์ปค์Šค๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. tabindex ์†์„ฑ ๊ฐ’์€ ์ˆซ์ž ์ธ๋ฐ, ์ด ์ˆซ์ž๋Š” Tab ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ์š”์†Œ ์‚ฌ์ด๋ฅผ ์ด๋™ํ•  ๋•Œ ์ˆœ์„œ ๋ฒˆํ˜ธ๊ฐ€ ๋œ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ์š”์†Œ๊ฐ€ ๋‘ ๊ฐœ ์žˆ์„๋•Œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ tabindex์—” 1์„, ๋‘ ๋ฒˆ์งธ ์š”์†Œ์˜ tabindex์—” 2๋ฅผ ํ• ๋‹นํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ํฌ์ปค์‹ฑ๋˜์–ด์žˆ๋Š” ์ƒํƒœ์—์„œ Tab์„ ๋ˆŒ๋ €์„ ๋•Œ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋กœ ํฌ์ปค์Šค๊ฐ€ ์ด๋™๋˜๊ฒŒ ๋œ๋‹ค.

<div tabindex="1">์ฒซ๋ฒˆ์งธ ํฌ์ปค์Šค</div>
<div tabindex="2">๋‘๋ฒˆ์งธ ํฌ์ปค์Šค</div>
<div tabindex="3">์„ธ๋ฒˆ์งธ ํฌ์ปค์Šค</div>

 

tabindex ๊ทœ์น™

tabindex์˜ ์†์„ฑ ๊ฐ’์— ๋”ฐ๋ผ ํฌ์ปค์‹ฑ ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. ๊ทœ์น™์ด ์กฐ๊ธˆ ๋‚œํ•ดํ•œ ํŽธ์ด๋‹ค.

  • tabindex๊ฐ€ ์—†๋Š” ์š”์†Œ : ๋ฌธ์„œ ๋‚ด ์ˆœ์„œ์— ๋”ฐ๋ผ ํฌ์ปค์Šค๊ฐ€ ์ด๋™ (๊ธฐ๋ณธ ์ˆœ์„œ)
  • tabindex๊ฐ€ 0์ธ ์š”์†Œ : ์ด ์š”์†Œ๋Š” tabindex ์†์„ฑ์ด ์—†๋Š”๊ฒƒ์ฒ˜๋Ÿผ ๋™์ž‘. ๋”ฐ๋ผ์„œ ํฌ์ปค์Šค๋ฅผ ์ด๋™์‹œํ‚ฌ ๋•Œ tabindex๊ฐ€ 0์ธ ์š”์†Œ๋Š” tabindex๊ฐ€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์š”์†Œ๋ณด๋‹ค ๋‚˜์ค‘์— ํฌ์ปค์Šค๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค.
  • tabindex๊ฐ€ -1์ธ ์š”์†Œ : ์˜ค๋กœ์ง€ focus() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์Šคํฌ๋ฆฝํŠธ๋กœ๋งŒ ํฌ์ปค์Šค ํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์— ์‚ฌ์šฉ
<div tabindex="1">์ฒซ๋ฒˆ์งธ ํฌ์ปค์Šค</div>
<div tabindex="0">์„ธ๋ฒˆ์งธ ํฌ์ปค์Šค</div>
<div tabindex="2">๋‘๋ฒˆ์งธ ํฌ์ปค์Šค</div>
<div tabindex="-1">ํฌ์ปค์Šค ์•ˆ๋จ</div>

See the Pen focus, blur - 3 by barzz12 (@inpaSkyrim) on CodePen.


# ์ฐธ๊ณ ์ž๋ฃŒ

https://www.geeksforgeeks.org/html-dom-onblur-event/

https://o7planning.org/12309/javascript-focusevent

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