[Mission 6] ๊ตฌํ (๋์ ๊ณผ์ )
โจ ๊ตฌํ ๊ธฐ๊ฐ : 11/26~27
โ๏ธ ๋ฆฌ๋ทฐ ๋๊ธ ์ญ์
[detail.js]function uploadComment() { const box = document.querySelector(".writeComment"); const trashBtn = document.createElement("button"); const dislikeIcon = document.createElement("i"); trashBtn.setAttribute('class', 'trash1'); trashBtn.innerHTML = "<i class='fas fa-trash-alt'></i>"; // appendChild ์์ฑ์ผ๋ก ์์ ์์๋ก ์ถ๊ฐํด์ค comments.appendChild(trashBtn); trashBtn.onclick = function (e) { box.removeChild(comments); }; };
[ Result ]
classList.add() ์์ฑ์ผ๋ก ์๋ก ์์ฑํ ๋๊ธ์ ํด๋์ค๋ฅผ ๋ถ์ฌํ๋ ค๊ณ ํ์ผ๋ ์ ์ฉ๋์ง ์๋ ์ค๋ฅ๋ฅผ ๋ฐ๊ฒฌํ๋ค. ๊ทธ๋์ ์ฐพ์๋ณด๋ ๋์ ์ผ๋ก ์์ฑํ ์์์๋ classList.add ์์ฑ์ ์ฌ์ฉํ ์ ์๊ณ setAttribute() ์์ฑ์ ์ฌ์ฉํด์ผ ํ๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค. ์ด๋ฅผ ์ด์ฉํด ์ญ์ ๋ฒํผ์ css ์์ฑ์ ๋ถ์ฌํด์ฃผ๊ณ onclick ์ด๋ฒคํธ๋ฅผ ์ฃผ์ด ์ญ์ ํ ์ ์๊ฒ ๋ง๋ค์๋ค.
โ๏ธ ๋ฆฌ๋ทฐ๋ณ ์ข์์ ๋ฒํผ์ ๋๋ฅด๋ ๊ธฐ๋ฅ
[detail.js]function uploadComment() { const dislikeIcon = document.createElement("i"); dislikeIcon.className = "far fa-heart"; dislikeIcon.onclick = clickHeart; comments.appendChild(dislikeIcon); function clickHeart() { const heart = window.event.target; if (heart.className == 'far fa-heart') { heart.className = 'fas fa-heart'; heart.style.color = '#c73d3d'; } else { heart.className = 'far fa-heart'; heart.style.color = '#929292' } } };
[ Result ]
onclick ํจ์์ classList.add(.hide)์ classList.remove(.hide)๋ก ์ ์ฉํ์๋๋ ๋งจ ์ ํํธ๋ง ๋ฐ๋๋ ํ์์ด ๋ฐ์ํ์๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ์ฌ ๊ฐ๊ฐ์ ํํธ์ ๋ค๋ฅธ ํด๋์ค ์ด๋ฆ์ ์ง์ ํด์ฃผ๊ณ ๊ทธ์ ๋ฐ๋ฅธ ๊ฐ๊ฐ์ ๋ณ์๋ฅผ ์์ฑํด์ฃผ์ด ์ ์ฉํ์๋ค.
๐ฌ ย ๊ทผ๋ณธ์ ์ธ ๋ฌธ์ ๋ ํด๊ฒฐ๋์์ผ๋ ์ฝ๋์ ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๊ฒ ๋์ด ๋ ๋์ ๋ฐฉ๋ฒ์ ์ฐพ์๋ด์ผ ๋ ๊ฒ ๊ฐ๋ค...!
์๋ ์๋ ๋๊ธ์ ํํธ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ํจ์๋ฅผ ์์ฑํ์ฌ ์ ์ฉํ๋ ค๊ณ ํ์ผ๋ ์ฒ์์ ์ฑ์์ง ํํธ์ hide ํด๋์ค๊ฐ ์ ์ฉ๋์ง ์๋ ๋ฌธ์ ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋์ ์ผ๋ก ์์ฑ๋ ์์์ด๋ฏ๋ก setAttribute๋ก ์ ์ฉํ๋ ค๊ณ ํ์ผ๋ ์ด๊ฒ๋ ์ ์ฉ๋์ง ์์๋ค. ๊ทธ๋์ ์๋ก์ด ํจ์๋ก clickHeart๋ฅผ ๋ง๋ค๊ณ window.event.target์ heart๋ก ๋ง๋ค์ด ์กฐ๊ฑด๋ฌธ์ ์ ์ฉํด์ฃผ์๋๋ ํด๊ฒฐ๋์๋ค.
โ๏ธ ์์ด๋์ ํจ์ค์๋๊ฐ ์กฐ๊ฑด์ ๋ง์ ์ border๋ฅผ ์ด๋ก์์ผ๋ก ๋ณ๊ฒฝํด์ฃผ๋ ๊ธฐ๋ฅ
[login.js]const idBox = document.getElementById('loginId'); const pwBox = document.getElementById('loginPw'); idBox.onkeyup = function () { const userId = document.querySelector('.id'); if (userId.value.includes('@')) { userId.style.borderColor = 'green'; } else { userId.style.borderColor = '#e3e3e3'; } }; pwBox.onkeyup = function () { const userPw = document.querySelector('.pw'); const regexPw = /(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#?!@$%^&*-]).{8,20}/; if (regexPw.test(userPw.value)) { userPw.style.borderColor = 'green'; } else { userPw.style.borderColor = '#e3e3e3'; } };
[ Result ]
์กฐ๊ฑด์ ๋ง๋ ๋น๋ฐ๋ฒํธ์ผ ๋ ํ ๋๋ฆฌ ์ ๋ณ๊ฒฝํ๋ ํจ์๋ฅผ ์์ฑํ ๋ ์ ๊ท์์ ํตํด ๊ตฌํํ๋ค๋ ์ ์ ์์๊ณ ์ ๊ท์์ ๋ํด ์ฐพ์๋ณด์๋๋ฐ ์ฌ๋๋ง๋ค ์ฐ๋ ๋ฐฉ์์ด ๋ฌ๋ผ์ ํ์ ํ๊ธฐ ์ด๋ ค์ ๋ค. ๊ทธ๋์ ์ฌ๋ฌ ์ ๊ท์์ ์ฐธ๊ณ ํ์ฌ ๋ด๊ฐ ๊ตฌํํ ํ์ด์ง์ ๋ง๋ ํ์์ ์ฐพ์์ ์ ์ฉํ์๋ค!
์กฐ๊ฑด์ ๋ง๊ฒ ํจ์๊ฐ ์ ์๋๋๋ input ๋ฐ๊นฅ์ ๋๋ฌ์ผ ์ด๋ก์์ผ๋ก ๋ณด์ด๋ ํ์์ด ๋ฐ์ํ๋ค. ์ฐพ์๋ณด๋ input ํ๊ทธ์๋ outline ์์ฑ์ด ๊ธฐ๋ณธ์ผ๋ก ์ฃผ์ด์ ธ ์์ด์ ๊ทธ ์์ฑ์ ์์ ์ฃผ์ด์ผ ๋ฐ๋ก ๋ณด์ธ๋ค๊ณ ํ๋ค๋ ๊ฒ์ ๋ณด๊ณ css์์ input ํ๊ทธ์ ์์ฑ์ outline์ none์ผ๋ก ์ค์ ํ์๋๋ ํด๊ฒฐ๋์๋ค!
โ๏ธ ๋น๋ฐ๋ฒํธ hide, show ๊ธฐ๋ฅ
[login.js]show.addEventListener('click', e => { if (e.target.className == "fa fa-eye eye") { e.target.className = "far fa-eye-slash eye"; pwInput.setAttribute('type', 'text'); } else { e.target.className = "fa fa-eye eye"; pwInput.setAttribute('type', 'password'); } });
[ Result ]
๋ฒํผ์ ํด๋ฆญํ์ ๋ input์ ๊ฐ์ ๋ณด์ด๊ฒ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํ๋๋ฐ ํ์๋์ input์ type์ด ๋ญ๋๊ณ ํ๋ ํ ๋ง๋์ ์ํ..! ํ๊ณ ๊นจ๋ซ๊ฒ ๋์๋ค. ๊ทธ๋์ pwInput์ setAttribute ์์ฑ์ ์ด์ฉํด ํ์ ์ ๋ณ๊ฒฝํด์ฃผ์๋๋ ํด๊ฒฐ๋์๋ค.
โ๏ธ ์ปคํผ๋ณ ์ข์์ ๋ฒํผ ๊ธฐ๋ฅ
[list.html]<section class="wrapper"> <div class="img"> <div class="scale"><img alt="ํ ํผ๋ ์ฝ๋ ๋ธ๋ฃจ" src="img/coffee.jpg" /></div> <p>ํ ํผ๋ ์ฝ๋ ๋ธ๋ฃจ <i class="far fa-heart" onclick="clickHeart();"></i></p> </div> </section>
[list.js]
like.onclick = clickHeart; function clickHeart() { const heart = window.event.target; if (heart.className == 'far fa-heart') { heart.className = 'fas fa-heart'; heart.style.color = '#c73d3d'; } else { heart.className = 'far fa-heart'; heart.style.color = 'black' } };
[ Result ]
๊ธฐ๋ฅ ์๋์ ์ ๋๋ ๋ ๋ฒ์งธ ํํธ๋ถํฐ๋ ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ๋ก ์ ์ฉ๋์ง ์๋ ํ์์ด ๋ฐ์ํ์ฌ ๊ทธ ์ด์ ๋ฅผ ์ฐพ๊ธฐ ์ํด html๊ณผ js ํ์ผ์ ์์ธํ ๋ณด์๋ค. ๋ช ์ค ๋์ง ์๋ ์ฝ๋์ฌ์ ๋ฆฌ๋ทฐ ๋๊ธ๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก ํจ์๋ฅผ ์ ์ฉํด์ฃผ์๋ค๊ณ ์๊ฐํ๋๋ฐ html์์ ์์ด์ฝ์ ๋ค๋ฅธ ํด๋์ค ์ด๋ฆ์ ์ถ๊ฐ๋ก ์ ์ฉํด์ฃผ์ด ์ฐจ์ด์ ์ด ์๊ธด ๊ฒ์ ๋ฐ๊ฒฌํ๊ณ ์ด๋ฅผ ์ง์์ฃผ์๋๋ ํด๊ฒฐ๋์๋ค!
๐ฌ ย js์์๋ ํด๋์ค ์ด๋ฆ์ ์ฐ๋ ๊ฒ๋ณด๋ค ์์ด๋๋ฅผ ์ฌ์ฉํ๋ ์ต๊ด์ ๋ค์ด์!
โ๏ธ ๋ฐ์ํ์ผ๋ก ๊ตฌํํ๊ธฐ
- media ์ฟผ๋ฆฌ ์ฌ์ฉํด ํ๋ฉด์ด ์ค์ด๋ค์์ ๋ ํด๋น ์ฌํญ ์ ์ฉ@media screen and (max-width: 850px) { .header { display: flex; flex-direction: column; align-items: flex-start; padding: 8px 24px; height: 90px; } .headerMenu { width: 100%; display: flex; flex-direction: column; align-items: center; } .headerMenu li { width: 50%; } .header h2 { width: 100%; display: flex; justify-content: flex-start; margin: -5px 0 0 10px; } }
[ Result ]
๐ฌ ย ๋ฐ์ํ ์น ๊ตฌ์ฑ์ ์์ง ๋ถ์กฑํ ๋ถ๋ถ์ด ๋ง์์ ๋ ๊ณต๋ถ๊ฐ ํ์ํ ๊ฒ ๊ฐ๋ค..!