230327 - eventHandler

๋ฐฑ์Šน์—ฐยท2023๋…„ 3์›” 27์ผ
1

๐Ÿšฉ javascript event

eventHandler

  • onblur : focus์—์„œ ๋ฒ—์–ด๋‚ฌ์„ ๋•Œ ์ž‘๋™๋จ

๐Ÿ“ ์„ค๋ช…

  • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ๋˜๋ฉด ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋œปํ•œ๋‹ค.


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

์˜ˆ์ œ1

css

* { margin: 0; padding: 0; }
body { background: #BDCDD6; }
body > div { width: 400px; margin: 150px auto; padding: 2px; border: 3px solid #7170b4; border-radius: 15px; }
h1 { margin: 10px 0; text-align: center; }
h2 { color: #5f689e; margin-bottom: 30px; text-align: center; }
h1 img { width: 150px; }

/* ์‚ฌ์šฉ์ž ์ง€์ • */
#feedback_wrap { height: 30px; }
#feedback {
  padding-left: 30px;
  background: url("../img/event_alarm.png") no-repeat;
}

form {
  padding: 0 50px 20px; /* ์ƒ ์ขŒ์šฐ ํ•˜ */
}
label { 
  display: block;
  padding: 20px 0 5px;
}
input[type="text"],
input[type="pw"] {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 5px;
}

input[type="text"]:focus,
input[type="pw"]:focus { 
  outline: none; 
  background: #e7f6ff;
}

.submit { 
  padding-top: 10px;
  text-align: right;
}
input[type="submit"] {
  padding: 10px;
  border: none;
  border-radius: 5px;
  background: #849dca;
  color: #fff;
}
input[type="submit"]:hover { background: #5f689e; }

html

<div id="page">
  <h1><img src="./img/kiwi_icon.png" alt="" /></h1>
  <h2>๋‘ ๋ฒˆ์งธ ์ œ๋ชฉ</h2>
  <form action="">
    <div id="feedback_wrap">
      <div id="feedback">ํ”ผ๋“œ๋ฐฑ์ด ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„</div>
    </div>

    <label for="username">์‚ฌ์šฉ์ž ์ด๋ฆ„ : </label>
    <input type="text" id="username"> <!-- label์— ์—ฐ๊ฒฐ -->

    <label for="pw">๋น„๋ฐ€๋ฒˆํ˜ธ : </label>
    <input type="text" id="pw"> <!-- label์— ์—ฐ๊ฒฐ -->

    <div class="submit">
      <input type="submit" value="LOG IN">
    </div>
  </form>
</div>

js

//์˜ˆ์ œ 1 - html ํƒœ๊ทธ์— ์ง์ ‘ ์‚ฌ์šฉ
// ๋ชจ๋“  ์˜ˆ์ œ์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„
const elName = document.getElementById("username");
const elMsg = document.getElementById("feedback");
let myName;

function checkName() {  
  // ์˜ˆ์ œ 1 ~ 4์˜ if๋ฌธ
  if (elName.value.length < 5) {
    elMsg.textContent = "์ด๋ฆ„์€ 5๊ธ€์ž ์ด์ƒ ์จ์ฃผ์„ธ์š”";
  }
  
  // ์˜ˆ์ œ 5์˜ if๋ฌธ
  myName = elName.value; // ๋ฐ›์•„์˜จ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ myName ๋ณ€์ˆ˜์— ๋„ฃ๋Š”๋‹ค.

  if (elName.value.length < minLength) {
    elMsg.textContent = `${myName}๋‹˜, ์ด๋ฆ„์€ ${minLength}๊ธ€์ž ์ด์ƒ ์จ์ฃผ์„ธ์š”`;
  }
}


// ์˜ˆ์ œ 2 script ์ถ”๊ฐ€. html ํƒœ๊ทธ์— onblur ์—†์„ ๋•Œ ์‚ฌ์šฉ
// elName.onblur = checkName();
// ํ•จ์ˆ˜์— ()๊ฐ€ ์žˆ์œผ๋ฉด ์ด๋ฒคํŠธ๋กœ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋กœ๋“œ๋  ๋•Œ ํ•จ์ˆ˜๊ฐ€ ๋ฐœ์ƒ๋จ
elName.onblur = checkName;

// ์˜ˆ์ œ 3 script ์ถ”๊ฐ€
elName.addEventListener("blur", checkName);
// Element.addEventListener("event", function);

// ์˜ˆ์ œ 4 script ์ถ”๊ฐ€ (IE9์ดํ•˜ ๋ฒ„์ „์˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ)
// elName.addEventListener("event", function) - IE9 ์ด์ƒ ๋ฒ„์ „
if (elName.addEventListener) { // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ธ์‹ํ–ˆ์„ ๋•Œ
  elName.addEventListener("blur", checkName)
} else {
  elName.attachEvent("blur", checkName)
  // attachEvent() : JS ์ดˆ์ฐฝ๊ธฐ Web API
}

// ์˜ˆ์ œ 5 script ์ถ”๊ฐ€
// elName.addEventListener("event", function) - IE9 ์ด์ƒ ๋ฒ„์ „
elName.addEventListener("blur", function() {
  checkName(myName, 7);
});
// ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๋–„๋Š” ์ง์ ‘ ()๋ฅผ ์“ธ ์ˆ˜ ์—†์–ด์„œ function(){}์„ ์ด์šฉ

// ์˜ˆ์ œ 6 script ์ถ”๊ฐ€ - focus()
function autoFocus() {
  elName.focus();
}
// ํ™”๋ฉด์ด ์‹คํ–‰๋  ๋•Œ ์ž‘๋™
window.addEventListener("load", autoFocus); 
// ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ. ์•„๋ž˜ ์ฝ”๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ•œ๋ฒˆ์— ์ง€์ •ํ•œ ๊ฒƒ
// window.addEventListener("load", function() {
//   elName.focus();
// });

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

์˜ˆ์ œ 1 ~ 4

์˜ˆ์ œ 5, 6


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€