const contSelect = document.querySelector('.cont-select');
const btnSelect = document.querySelector('.btn-select');
const listMember = document.querySelector('.list-member');
const option = document.querySelectorAll('.opt-member');
// ํ ๊ธ๋ฒํผ์ด ํด๋ฆญ --> ๋ฉ๋ด ๋ณด์ธ๋ค
btnSelect.addEventListener('click', function(){
listMember.classList.toggle('show');
btnSelect.classList.toggle('on');
})
btnSelect.addEventListener('blur', function(){
listMember.classList.remove('show');
btnSelect.classList.remove('on');
})
option.forEach(function(item){
item.addEventListener('mousedown',function(e){
const value = e.currentTarget.textContent;
btnSelect.textContent = value;
btnSelect.classList.add('selected-style')
})
})
๋๋๊ฒ๋ ์ด ๋ค ๊ฐ์ ์ด๋ฒคํธ์๋ ๊ด๊ณ๊ฐ ์ฝํ์๋ค.
1) mousedown + mouseup = click๊ณผ ๊ฐ๋ค.
2) ์คํ ์ฐ์ ์์: mousedown > blur > mouseup > click
๊ฐ์ ์ ๋ฌํ ๋, e.target.textConten
๋ก ์ฌ์ฉํ๋ค๋ฉด, textContent
๋ ์ด๋ฒคํธ์ ํ๊ฒ์ ์์์์์ ๋ชจ๋ ๊ฐ์ ๋ถ๋ฌ์จ๋ค. ๊ทธ๋์ ์์ํ ๋, ์กฐ์ฌํด์ผ ํ๋๋ฐ, event.target.nodeName == 'BUTTON'
์ฒ๋ผ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํ๋ค.
js๋ก ์ง์ style ์ง์ ํ์ง ๋ง๊ธฐ, ๋์ค์ css๋ก ์คํ์ผ ์ ์ง ๋ณด์๊ฐ ํ๋ค์ด์ง
css ์คํ์ผ์ JS๋ก ๊ณ์ฐํ์ฌ ๋ฃ๋ ๊ฒฝ์ฐ์ ์คํ์ผ ๋ณ๊ฒฝ์ด ์์ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ style์ ์ฌ์ฉํ์ง ๋ง์. ์ฆ, class๋ฅผ ์์ฑํ๊ณ ์ง์ฐ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข๋ค
creatElement
๋ก JS๋ฅผ ํตํด ์์๋ฅผ ์์ฑํ๋ ์ด์ ๋ ๋์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ์ฌ์ฉํ ๋, ์์ ์ด ๋น๋ฒํ๊ฒ ์๊ธธ ์ ์๋ค. ์ด๋ HTML๋ก ์์๋ฅผ ์์ฑํ๋ฉด ๊ณ ์ ๋๊ธฐ ๋๋ฌธ์ ์ ์ง/๋ณด์๊ฐ ์ฉ์ดํ์ง ์๋ค.const btn = document.querySelector('.btn-select');
const list = document.querySelector(".list-member");
const arrLang = ['Python','Java','JavaScript','C#','C/C++'];
// ์์ค๋ก ์์๋ฅผ ๋ง๋๋ ์ด์
//๋ฐฑ์๋์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ฉด(์ง๊ธ์ arrLang ๋ฐฐ์ด) ๋ฐ์ด๋๋ ์์๋ฅผ ์์ ํ ๋๊ฐ ์๊ธธ ์ผ์ด ๋น๋ฒํ๋ฐ, html๋ก ๋ฐ์๋์ผ๋ฉด staticํด์ ธ์ ๊ด๋ฆฌ๊ฐ ์ด๋ ค์ ์์๋ฅผ JS๋ก ๋ง๋๋ ๊ฒ์.
arrLang.forEach((item)=> {
const li = document.createElement('li');
const btn = document.createElement('button');
btn.setAttribute('type','button');
//<button type = "button"></button>
btn.textContent = item;
// btn.classList.add('') = btn.setAttribute('class','');
li.appendChild(btn);
list.appendChild(li);
});
btn.addEventListener('click', () => {
btn.classList.toggle('on');
});
list.addEventListener('click', (event) => {
if (event.target.nodeName === 'BUTTON') {
btn.textContent = event.target.textContent;
btn.classList.remove('on');
}
});