<input type="text">
<div id="parentElement">
<p id="childElement">ใ
ใดใ
ใน</p>
</div>
<script>
var input = document.querySelector("input");
var span = document.createElement("span");
// input์ผ๋ก ์ฃผ๋ฉด ์ค์๊ฐ์ผ๋ก ์
๋ ฅ ๋ฐ์์ด.
input.addEventListener('input, () => {
span.textContent = input.value;
});
var sibling = document.getElementById("childElement");
var parentDiv = document.getElementById("parentElement");
parentDiv.insertBefore(span, sibling);
</script>
<strong class="sayHi">
๋ฐ๊ฐ์ต๋๋ค.
</strong>
<script>
const sayHi = document.querySelector('.sayHi');
// ์๋ ์์์ ์์ ์๋ค๋ก ์์๋ฅผ ๋ฃ์ ์ ์๋ค.
sayHi.insertAdjacentHTML('beforebegin', '<span>1</span>');
sayHi.insertAdjacentHTML('afterbegin', '<span>2</span>');
sayHi.insertAdjacentHTML('beforeend', '<span>3</span>');
sayHi.insertAdjacentHTML('afterend', '<span>4</span>');
</script>
๋ ธ๋ : ๊ฐ ์์, ์์ฑ, ์ฝํ ์ธ ๋ฅผ ํํํ๋ ๋จ์๋ฅผ ๋ชจ๋ ๋ ธ๋๋ผ๊ณ ํ๋ค. ์ฃผ์, Text๊ฐ, ์ค๋ฐ๊ฟ ...
const cont = document.querySelector(".cont");
console.log(cont.firstElementChild); // ์ฒซ๋ฒ์งธ ์์
console.log(cont.lastElementChild); // ๋ง์ง๋ง ์์
console.log(cont.nextElementSibling); // ๋ค์ ํ์ ์์
console.log(cont.previousSibling); // ์ด์ ํ์ ๋
ธ๋ - Element๊ฐ ์์ผ๋ฉด.
console.log(cont.children); // ๋ชจ๋ ์ง๊ณ์์
console.log(cont.parentElement); // ๋ถ๋ชจ ์์
- ์ด๋ฒคํธ ๊ฐ์ฒด : ์ด๋ฒคํธ์์ ํธ์ถ๋๋ ํจ์์๋ ์ด๋ฒคํธ์ ๊ด๋ จ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ ์ธ์๊ฐ ์ ์ก๋๋ค. ์ก์์ ์ธ ์ ์๋ค.
- ์ด๋ฒคํธ ํ๋ฆ : ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ DOM tree๋ฅผ ํ๊ณ ์ด๋ฒคํธ ๋์์ ์ฐพ์๊ฐ. (์บก์ฒ๋ง ๋จ๊ณ) ๋์์ ์ฐพ๊ณ ์บก์ฒ๋ง์ด ๋๋๋ฉด DOM tree๋ฅผ ํ๊ณ ์ฌ๋ผ๊ฐ๋ฉด์ ๋ชจ๋ ๋ฒ๋ธ๋ง ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์คํ (์ด๋ฒคํธ๋ฒ๋ธ๋ง ๋จ๊ณ) ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์ฐจ๋ก๋ก ์คํ๋๋ ๊ฒ์ ์ด๋ฒคํธ ์ ํ(event propagation)๋ผ๊ณ ํ๋ค.
window.addEventListener('click', () => {
console.log("window capture!");
}, true); // true : ์บก์ฒ๋ง ๋จ๊ณ์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋๋ก. ์์ฐ๋ฉด ๋ฒ๋ธ๋ง ์ด๋ฒคํธ default
<article class="parent">
<ol>
<li><button class="btn-first" type="button">๋ฒํผ1</button></li>
<li><button type="button">๋ฒํผ2</button></li>
<li><button type="button">๋ฒํผ3</button></li>
</ol>
</article>
<script>
const parent = document.querySelector('.parent');
parent.addEventListener('click', function (event) {
console.log(event.target); // ์ด๋ฒคํธ๋ฅผ ์ผ์ผํจ ๋
์. ๋ถ๋ชจ์ ์ด๋ฒคํธ๊ฐ ๋ถ์ด์์ผ๋ฉด ์์์์ ์ด๋ฒคํธ๊ฐ ์ผ์ด๋๋ ์คํ๋๊ธฐ ๋๋ฌธ์!
console.log(event.currentTarget); // ์๋ ์ด๋ฒคํธ๊ฐ ๋ถ์ด์๋ ๋
์.
//
// ์ด๋ฒคํธ ์์ : target์ผ๋ก ์ด๋ฒคํธ๋ฅผ ์ผ์ผํจ ๋
์์ ์ก๊ณ ๊ทธ๊ฒ์ ๋ํ ์ฒ๋ฆฌ๋ ๊ฐ๋ฅ!
if (event.target.classList.contains("btn")) {
event.target.innerText = "๋ฒํผ~~";
})
</script>
const parent = document.querySelector('.parent');
parent.addEventListener('click', function (event) {
//currentTarget === this
console.log(this);
})
parent.addEventListener('click', (evt) => {
// ํ์ดํ ํจ์ ์์ this๋ ์์ ์ค์ฝํ์ this๋ฅผ ์ฐธ์กฐ. -> ํท๊ฐ๋ฆฌ๋๊น ์ฌ๋งํ๋ฉด this ๋์ evt.target ์ด๋ ๊ฒ ์จ์ฃผ์.
console.log(this);
})
#20 ์์ js ์ฝ๋๋ ์ ์๋์ด ์ฃผ์๊ณ CSS๋ ์ง์ ์์ฑํ์๋, ์ ๋ ํธ ๋ฐ์ค๋ฅผ ์ด๋ฒ์๋ js ์ฝ๋๋ฅผ ์ง์ ์์ฑํด๋ณด์๋ค.
// 1. ul์ li ๋์ ์ถ๊ฐ
const memberList = document.body.querySelector(".list-member");
const contents = ["Python", "Java", "JavaScript", "C#", "C/C++"];
for (x of contents) {
const li = document.createElement('li');
const button = document.createElement('button');
button.type = "button";
button.innerText = x;
li.appendChild(button);
memberList.appendChild(li);
// list.appendChild(li).appendChild(btn); //chaining ๊ฐ๋ฅ!!
}
// 2. addEventListener ๋ถ์ด๊ธฐ.
const selectButton = document.querySelector('.btn-select');
const memberButtons = document.querySelectorAll('.list-member li button');
// ๋ฐ์ค ๋๋ฅด๋ฉด option ๋์ค๊ฒ
selectButton.addEventListener('click', (event) => {
selectButton.classList.toggle('on');
});
// option ์ ํํ๋ฉด ๋๋ฆฌ๋ฉด์ option list ๋ซํ๊ฒ.
// forEach ๋ ๋ฐฐ์ด์ ์ํํ๋ ๊ฒ์ธ๋ฐ..
// querySelectorAll ๋ก ๋ฝ์์จ๊ฑด nodeList(์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด)
// Array.prototype.forEach, NodeList.prototype.forEach() ๊ฐ ๋ ๋ค ์์ด์ ์๋์ด ๋๋ ๊ฒ.
// ๊ทผ๋ฐ NodeList forEach๋ IE ์ง์์ด ์๋จ..............
memberButtons.forEach(x => x.addEventListener('click', (event) => {
selectButton.textContent = event.target.innerText;
selectButton.classList.remove('on');
}));
// keyboard ์ ๊ทผ์ฑ์ ๊ณ ๋ คํ ์ฝ๋. const listAll = ul.querySelectorAll('button'); // option list const firstOption = ul.querySelector('button'); //option first button const lastOption = listAll[listAll.length - 1]; //option last button //focus ์ด๋ const handleTabFoucus = function (e) { if (!e.shiftKey && e.keyCode === 9) { e.preventDefault(); window.setTimeout(function () { firstOption.focus(); }, 100); } }; //shift+tab const handlShiftTabFoucus = function (e) { if (e.shiftKey && e.keyCode === 9) { e.preventDefault(); window.setTimeout(function () { lastOption.focus(); }, 100); } }; //ESCํค const hadleEsc = function (e) { if (e.keyCode === 27) { btn.classList.remove('on'); ul.classList.remove('on'); window.setTimeout(function () { btn.focus(); }, 100); } } selectButton.addEventListener('keydown', hadleEsc); memberList.addEventListener('keydown', hadleEsc); lastOption.addEventListener('keydown', handleTabFoucus); firstOption.addEventListener('keydown', handlShiftTabFoucus);
// jQuery ์ฒ๋ผ ์ฐ๋ ํ ํฌ๋ -> ์ฐ์ง ๋ง์. const $ = (selector => document.querySelector(selector); $('.list-member')
Test Driven Development - ์ฐ๋ฆฌ๋ jasmin์ ์ด์ฉํ๋ค.
// ์๋ ์ฝ๋ function plusOne(num) { return num + 1; } // test code // ํ ์คํธ ์ ๋๋ค์ ๋ชจ์ describe('์์ค๋ฏผ ํ ์คํธ ์ ๋๋ค.', () => { // it : ํ ์คํธ ์ ๋ 1 (๋ง์ ํจ์) it('๋ง์ ์ ํ๋ ํจ์์ ๋๋ค.', () => { let num = 10; // expect : ์คํํ ํจ์์ ๊ฒฐ๊ณผ๊ฐ์ ์ธ์๋ก ์ ๋ฌํ๋ค. // toBe : ๋ด๊ฐ ๊ธฐ๋ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฃ์ผ๋ฉด ์ผ์นํ๋์ง ํ๋จํด์ค. expect(plusOne(num)).toBe(num+1); }); });
๊ทผ๋ฐ ๊ฐ๋จํ ์ฝ๋๋ผ์ ์ด๋ ๊ฒ ๋ฐ๋ก ์ง ๊ฑฐ์ง.. ๋ง์ฝ ๋ ๋ณต์กํ๋ค๋ฉด...? ์ ๋ง ํ๋ค ๊ฒ ๊ฐ๋ค. ๊ทธ๋์ ํ ์คํธ๊ฐ ๊ฐ๋ฅํ ์ฝ๋๋ฅผ ์ง๋ ค๋ฉด
- ์ฝ๋๋ฅผ UI ์์ ์์ ํ ๋ถ๋ฆฌ
- ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ JS ํ์ผ๋ก ๋ถ๋ฆฌ
- ๋ชจ๋ ๊ธฐ๋ฅ์ ํจ์๋ก ๊ตฌํ
ํ๋ ๊ฒ์ด ์ค์ํ๋ค.
+ ํ ์คํธ ์ฝ๋๋ ๊ฒฐ๊ตญ ์ฌ๋์ด ์ง๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ธ์ ๋ ์ง ์์ธ์ฌํญ์ด ๋ฐ์ํ ์ ์๋ค.