230322 - form

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

๐Ÿšฉ javascript

form ๊ด€๋ จ ์‘์šฉ ์˜ˆ์ œ

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

์ž…๋ ฅ

css

* { margin: 0; padding: 0; }
article { width: 60%; margin: 80px auto; }
h2 { padding: 10px 0; }
input { height: 30px; border: 1px solid #ccc; background: #f7f7f7 ; }
input:focus { 
  outline: none; 
  border: 1px solid #bbe2f1; 
  background: #daeff7;
}
button {
  height: 32px;
  border: 0;
  padding: 0px 20px;
  background: powderblue;
  cursor: pointer;
  vertical-align: bottom;
}
a { text-decoration: none; color: #111; }
li { list-style: none; padding: 10px; }

.form1 li {
  border-bottom: 1px solid #ccc;
}

html

<article class="form1">
  <h2>์ถœ์„์ฒดํฌ</h2>
  <div>
    <input type="text" />
    <button>์ €์žฅ</button>
  </div>
  <ul></ul>
</article>

js

const inputForm1 = document.querySelector(".form1 input")
const btnForm1 = document.querySelector(".form1 button")
const ulForm1 = document.querySelector(".form1 ul")

btnForm1.onclick = function(){
  if (inputForm1.value != "") { // ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด ์žˆ์„ ๋•Œ
    const createLi = document.createElement("li"); // liํƒœ๊ทธ ์ƒ์„ฑ
    createLi.append(inputForm1.value); // ์ƒ์„ฑ๋œ li์˜ input ์ž…๋ ฅ๊ฐ’์„ ์‚ฝ์ž… (๋งจ ๋’ค)   
    // value - ๊ฐ’
    ulForm1.prepend(createLi); // ์œ„์—์„œ ๋งŒ๋“  li๋ฅผ ul์— ์‚ฝ์ž… (๋งจ ์•ž)
    console.log(ulForm1); 
    inputForm1.value = ""; // ์ €์žฅ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด input์„ ๋น„์›€ (reset)
    inputForm1.focus(); // input์— focus๋ฅผ ๋งž์ถค
  }
};

์ถœ๋ ฅ

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








๐Ÿšฉ jQuery

attribute(์†์„ฑ) ๋ณ€๊ฒฝ ์˜ˆ์ œ

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

์ž…๋ ฅ

css

body {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  background: #d3d3d3;
}

/* header */
header { background: #fff; }
header div {
  max-width: 1000px;
  margin: auto;
  height: 80px;
}
header h1 {
  font-size: 3em; /* 16px * 3 */
  line-height: 80px;
}


/* main */
main { background: #818181; }
main > div { padding: 50px; text-align: center; }
main img { max-width: 1000px; }
button {  }

.popup { 
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .7);
  text-align: center;
}
.popup > div {
  display: inline-block;
  padding: 30px;
  background: #fff;
  margin-top: 100px;
}
.popup img { height: 60vh; }


/* footer */
footer { padding: 30px 0; } 
footer div {
  max-width: 1000px;
  margin: auto;
  text-align: right;
}
footer small {
  color: #fff;
  font-size: 0.8em;
}

html

<header>
  <div>
    <h1>jQuery - attr(์†์„ฑ๋ณ€๊ฒฝ)</h1>
  </div>
</header>

<main>
  <div>
    <img src="./img/cat2.jpg" alt="๊ณ ์–‘์ด" title="๊ณ ์–‘์ด"> <br><br>
    <button class="btn1">๊ณ ์–‘์ด</button>
    <button class="btn2">๊ฐ•์•„์ง€</button>
    <button class="btn3">์ƒˆ ์ฐฝ ์—ด๊ธฐ</button>
  </div>
</main>

<div class="popup">
  <div>
    <img src="./img/dog2.png" alt=""><br><br>
    <button>CLOSE X</button>
  </div>
</div>

<footer>
  <div><small>jQuery ์—ฐ์Šต ํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค.</small></div>
</footer>

jQuery

var elImg = $("main img").attr("src"); // ์ด๋ฏธ์ง€์˜ src์˜ value
console.log(elImg);

$(".btn1").click(function(){
  $("main img").attr({ "src": "img/cat2.jpg" , "alt": "๊ณ ์–‘์ด", "title": "" }); // title key๊ฐ’๋งŒ ๋‚จ์Œ
  $("main img").removeAttr("title"); // title ์ „์ฒด๊ฐ€ ์‚ฌ๋ผ์ง
});

$(".btn2").click(function(){
  $("main img").attr({ "src": "img/dog1.jpg" , "alt": "๊ฐ•์•„์ง€", "title": "ํ…Œ์ŠคํŠธ" });
});

$(".btn3").click(function(){
  $(".popup").fadeIn(1000);
})

$(".popup button").click(function(){
  $(".popup").fadeOut(200);
})



/*
  A.attr(์†์„ฑ๋ช…) - ์—˜๋ฆฌ๋จผํŠธ A์š”์†Œ์˜ ์†์„ฑ๊ฐ’์„ ๊ฐ€์ ธ์˜ด
  A.attr({ ์†์„ฑ๋ช…(key): ์†์„ฑ๊ฐ’(value) }) - A์š”์†Œ์˜ ์†์„ฑ์„ ์ œ์–ด/์ถ”๊ฐ€
  A.removeAttr(์†์„ฑ๋ช…) - A์š”์†Œ์— ์žˆ๋Š” ์†์„ฑ์„ ์ œ๊ฑฐ
*/

์ถœ๋ ฅ

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









class ๋ฉ”์„œ๋“œ ์‘์šฉ

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

์ž…๋ ฅ

css

body { font-family:Verdana, Geneva, Tahoma, sans-serif; background:#d3d3d3;}

header { background:#fff;}
header div {  max-width:1000px; margin:auto; height:80px;} 
header h1 { font-size:3em; line-height:80px;}



main { background:#929090; padding:100px; }

main ul::after { display: block; content: ""; clear:both; }
main li { float:left;  width:300px; height:300px; margin-bottom:30px; }
main li button { padding:5px 10px; border:none; border-radius:5px; margin-bottom:10px; cursor:pointer;}

.box { display:flex; justify-content:center; align-items:center; width:250px; height:250px; background:lavenderblush; }

.red { background: red; }
.orange { background:orange; }
.bd { border: 5px solid black; }


footer { padding:30px 0; }
footer div { max-width:1000px; margin:auto; text-align:right; }
footer small { color:#fff; font-size:0.8em; }

html

<header>
  <div><h1>jQuery - class ๋ฉ”์„œ๋“œ</h1></div>    
</header>

<main>
  <ul>
    <li class="method1">
      <button>addClass</button>
      <button>removeClass</button>
      <button>All</button>
      <div class="box">addClass / removeClass</div>
    </li>

    <li class="method2">
      <button>toggleClass</button>
      <div class="box">toggleClass</div>
    </li>

    <li class="method3">
      <button>orange</button>
      <button>border</button>
      <button>oragne+border</button>
      <button>reset</button>
      <div class="box"></div>
      โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…
    </li>
  </ul>
</main>

<footer>
  <div><small>jQuery์—ฐ์Šต ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค</small></div>
</footer>

jQuery

$(function () {
  $(".method1 button:nth-child(1)").click(function () {
    $(".method1 .box").addClass("red");
  });
  $(".method1 button:nth-child(2)").click(function () {
    $(".method1 .box").removeClass("red");
  });
  $(".method1 button:nth-child(3)").click(function () {
    $(".method1 .box").removeClass();
  });

  $(".method2 button").click(function () {
    $(".method2 .box").toggleClass("red bd");
    // ํด๋ž˜์Šค ์—ฌ๋Ÿฌ ๊ฐœ ์ ์šฉ ์‹œ
  });

  var $box3 = $(".method3 .box"); // ์„ ํƒ์ž๋ฅผ ๋ณ€์ˆ˜๋กœ
  $(".method3 button:nth-child(1)").click(function () {
    $box3.addClass("orange");
  });
  $(".method3 button:nth-child(2)").click(function () {
    $box3.addClass("bd");
  });
  $(".method3 button:nth-child(3)").click(function () {
    if ($box3.hasClass("orange")) {
      $box3.addClass("bd");
    } else if ($box3.hasClass("bd")) {
      $box3.addClass("orange");
    } else {
      $box3.html("<p>์ ์šฉ๋œ ํด๋ž˜์Šค๊ฐ€ ์—†์Œ</p>");
    }
  });

  $(".method3 button:nth-child(4)").click(function () {
    $box3.removeClass("orange bd").html(""); // ๋ฉ”์†Œ๋“œ ์ฒด์ธ
  });
});

/*
  A.addClass(b) - ์—˜๋ฆฌ๋จผํŠธ A์— ํด๋ž˜์Šค b ์ถ”๊ฐ€
  A.removeClass(b) - A์— ์ ์šฉ๋˜์–ด์žˆ๋Š” ํด๋ž˜์Šค b ์ œ๊ฑฐ
  A.removeClass() - A์— ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ํด๋ž˜์Šค ์ œ๊ฑฐ. ๊ด„ํ˜ธ๊ฐ€ ๋น„์–ด์žˆ์Œ
  A.toggleClass(b) - A์— ํด๋ž˜์Šค b๋ฅผ ์ถ”๊ฐ€. ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€๋Šฅ
*/

์ถœ๋ ฅ

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

๊ธฐ๋ณธ

addClass / removeClass


toggleClass

orange

border

์ ์šฉ๋œ ํด๋ž˜์Šค๊ฐ€ ์—†์„ ๋•Œ

reset







tab ๋ฉ”๋‰ด ์‘์šฉ

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

์ž…๋ ฅ

css

body {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  background: #d3d3d3;
}

header { background: #fff; }
header div {
  max-width: 1000px;
  margin: auto;
  height: 80px;
}
header h1 {
  font-size: 3em; /* 16px * 3 */
  line-height: 80px;
}

main { background: #818181; padding: 100px 0; }
.tab {
  width: 450px;
  margin: auto;
}
.tabMenu { display: flex; }
.tabMenu li { 
  flex: 1; /* 1:1:1 */
  border: 1px solid #505050;
  color: #fff;
  background: #aaa;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-right: none;
}
.tabMenu li:last-child { border-right: 1px solid #505050; }
.tabMenu li.active {
  background: #fff;
  color: #000;
}

footer { padding: 30px 0; } 
footer div {
  max-width: 1000px;
  margin: auto;
  text-align: right;
}
footer small {
  color: #fff;
  font-size: 0.8em;
}

html

<header>
  <div>
    <h1>jQuery - tab</h1>
  </div>
</header>

<main>
  <div class="tab">
    <ul class="tabMenu">
      <li class="active">MENU1</li>
      <li>MENU2</li>
      <li>MENU3</li>
    </ul>
  </div>
</main>

<footer>
  <div><small>jQuery ์—ฐ์Šต ํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค.</small></div>
</footer>

jQuery

$(".tabMenu li").click(function () {
  let num = $(this).index();
  console.log(num);

  $(".tabMenu li").removeClass("active"); // 3๊ฐœ์˜ li ์ „๋ถ€ ํด๋ž˜์Šค active ์ œ๊ฑฐ
  $(this).addClass("active");
});

/*
  A.index() - A๊ฐ€ ๋ช‡ ๋ฒˆ์งธ ์ˆœ์„œ์ธ์ง€ ์•Œ์•„์˜จ๋‹ค (0๋ถ€ํ„ฐ ์‹œ์ž‘)
*/

์ถœ๋ ฅ

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







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

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