Javascript ๊ธฐ์ดˆ

๋ณธ ํฌ์ŠคํŒ…์€ ๋…ธ๋งˆ๋“œ ์ฝ”๋”๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ณต๋ถ€ํ•˜๊ณ  ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

0. ๊ธฐ๋ณธ ์„ค์ •

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Momentum</title>
</head>
<body>
    <div>
        <h1 class='hello'>Grab me!</h1>
    </div>
    <script src="app.js"></script>
</body>
</html>

1. list ๋งŒ๋“ค๊ธฐ & ์›์†Œ ์ถ”๊ฐ€

1
2
3
4
5
6
7
8
const daysOfWeek = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat'];

// Get Item from Array
console.log(daysOfWeek);

// Add one more day to the array
daysOfWeek.push('sun');
console.log(daysOfWeek);

2. ์›์†Œ ์ฐพ๊ธฐ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
const hellos = document.getElementsByClassName('hello');
console.log(hellos);

const title = document.querySelector('#hello h1'); // ID
const title = document.querySelector('.hello h1'); // class
console.log(title); 

const title = document.querySelector('div.hello:first-child h1');
console.dir(title);
title.style.color = 'blue';
  • getElementsByClassName() : ๋งŽ์€ element๋ฅผ ๊ฐ€์ ธ์˜ฌ๋•Œ ์”€(array๋ฅผ ๋ฐ˜ํ™˜)
  • getElementsByTagName() : name์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ(array๋ฅผ ๋ฐ˜ํ™˜)
  • querySelector : element๋ฅผ CSS selector๋ฐฉ์‹์œผ๋กœ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Œ (ex. h1:first-child)
    ๋‹จ ํ•˜๋‚˜์˜ element๋ฅผ returnํ•ด์คŒ
    โ‡’ hello๋ž€ class ๋‚ด๋ถ€์— ์žˆ๋Š” h1์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ๋‹ค(id๋„ ๊ฐ€๋Šฅํ•จ)
  • ์ฒซ๋ฒˆ์งธ element๋งŒ ๊ฐ€์ ธ์˜ด
  • ์กฐ๊ฑด์— ๋งž๋Š” ์„ธ๊ฐœ ๋‹ค ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์œผ๋ฉด querySelectorAll
    โ‡’ ์„ธ๊ฐœ์˜ h1์ด ๋“ค์–ด์žˆ๋Š” array๋ฅผ ๊ฐ€์ ธ๋‹ค ์คŒ
  • querySelector("#hello); ์™€ getElementById(“hello”); ๋Š” ๊ฐ™์€ ์ผ์„ ํ•˜๋Š” ๊ฒƒ์ž„
    ํ•˜์ง€๋งŒ ํ›„์ž๋Š” ํ•˜์œ„์š”์†Œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ๋ชปํ•˜๋ฏ€๋กœ ์ „์ž๋งŒ ์“ธ๊ฑฐ๋‹ค
    (์ถœ์ฒ˜: https://nomadcoders.co/javascript-for-beginners/lectures/2892)

3. ์ด๋ฒคํŠธ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const h1 = document.querySelector('div.hello:first-child h1');

function handleTitleClick() {
    // console.log('title was clicked!');
    h1.style.color = 'blue';
}

function handleMouseEnter() {
    // console.log('mouse is here!');
    h1.innerText = 'Mouse is here!';
}

function handleMouseLeave() {
    // console.log('mouse is leaving');
    h1.innerText = 'Mouse is gone!';
}

function handleWindowResize() {
    document.body.style.backgroundColor = 'tomato';
}

function handleWindowCopy() {
    alert('copier!');
}

function handleWindowOffline() {
    alert('SOS no WIFI');
}

function handleWindowOnline() {
    alert('ALL GOOD');
}

h1.addEventListener('click', handleTitleClick);
h1.addEventListener('mouseenter', handleMouseEnter);
h1.addEventListener('mouseleave', handleMouseLeave);
// title.onclick = handleTitleClick;
// title.onmouseenter = handleMouseEnter;

window.addEventListener('resize', handleWindowResize);
window.addEventListener('copy', handleWindowCopy);
window.addEventListener('offline', handleWindowOffline);
window.addEventListener('Online', handleWindowOnline);
  • ๋ฒ„ํŠผ์ด ์•„๋‹ˆ์–ด๋„, h1์—ฌ๋„ ํด๋ฆญ์ด ๊ฐ€๋Šฅํ•˜๋‹ค!

4. CSS in Javascript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const h1 = document.querySelector('div.hello:first-child h1');

function handleTitleClick() {
    const currentColor = h1.style.color;
    let newColor;
    if(currentColor === 'blue'){
        newColor = 'tomato';
    } else {
        newColor = 'blue';
    }
    h1.style.color = newColor;
}

h1.addEventListener('click', handleTitleClick);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  const h1 = document.querySelector('div.hello:first-child h1');
  
  function handleTitleClick() {
      if (h1.className === 'active') {
          h1.className = '';
      } else {
          h1.className = 'active';
      }
  
  }
  
  h1.addEventListener('click', handleTitleClick);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  body {
      background-color: beige;
  }
  
  h1 {
      color: cornflowerblue;
      transition: color .5s ease-in-out;
  }

  .active {
      color: tomato;
  }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const h1 = document.querySelector('div.hello:first-child h1');

function handleTitleClick() {
    h1.classList.toggle('clicked') // ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.
    // const clickedClass = 'clicked';
    // if (h1.classList.contains(clickedClass)) {
    //     h1.classList.remove(clickedClass);
    // } else {
    //     h1.classList.add(clickedClass);
    // }
}

h1.addEventListener('click', handleTitleClick);

4-0. Input Values

1
2
3
4
5
6
7
8
  const loginInput = document.querySelector('#login-form input');
  const loginButton = document.querySelector('#login-form button');
  
  function onLoginBtnClick() {
      console.log(loginInput.value);
  }
  
  loginButton.addEventListener('click', onLoginBtnClick);
1
2
3
4
5
6
7
  <body>
  <div id='login-form'>
      <input type='text' placeholder='What is your name?' />
      <button>Log In</button>
  </div>
  <script src="app.js"></script>
</body>

4-1. Form Submission

1
2
3
4
5
6
7
8
<body>
    <form id='login-form'>
        <input required maxlength="15" type='text' placeholder='What is your name?' />
        <!-- <button>Log In</button> -->
        <input type='submit' value='Log In' />
    </form>
    <script src="app.js"></script>
</body>
  • input์˜ ์œ ํšฝ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” input์ด form ์•ˆ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค.

4-2. Events

1
2
3
4
5
6
7
8
9
const loginForm = document.querySelector('#login-form');
const loginInput = document.querySelector('#login-form input');

function onLoginSubmit(event) {
  event.preventDefault();
  console.log(loginInput.value);
}

loginForm.addEventListener('submit', onLoginSubmit);
1
2
3
4
5
6
function onLoginSubmit(event) {
  event.preventDefault();
  const username = loginInput.value;
  loginForm.classList.add('hidden');
  console.log(username);
}

4-3. ์‚ฌ๋ผ์ง€๋Š” ๋กœ๊ทธ์ธ Form ๋งŒ๋“ค๊ธฐ

1
2
3
.hidden {
  display: none;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const loginForm = document.querySelector('#login-form');
const loginInput = document.querySelector('#login-form input');

function onLoginSubmit(event) {
  event.preventDefault();
  const username = loginInput.value;
  loginForm.classList.add('hidden');
  console.log(username);
}

loginForm.addEventListener('submit', onLoginSubmit);