Unit 2. ์น ๋ฌธ์์ ์๋ฐ ์คํฌ๋ฆฝํธ

5-1. ํ ๊ธ์ฌ์ฉํ์ฌ ์์ธ ์ค๋ช ์ด๊ธฐ/๋ซ๊ธฐ
const viewBtn=document.querySelector("#view");
const detail=document.querySelector("#detail");
viewBtn.addEventListener("click",()=>{
detail.classList.toggle("hidden");
})

โ point
์ด๋ฆ.classList.toggle("ํด๋์ค ๋ช ")
5-2. ์ต๋๊ณต์ฝ์ ๊ตฌํ๊ธฐ
const num1=document.querySelector("#number1");
const num2=document.querySelector("#number2");
const btn=document.querySelector("#calc");
const result=document.querySelector("#result");
let gcdNum=1;
btn.addEventListener("click",()=>{
n1=parseInt(num1.value);
n2=parseInt(num2.value);
isNum(n1,n2);
});
//์ต๋๊ณต์ฝ์
function GCD(a,b){
let min=(a>=b)?b:a;
for(let i=0;i<=min;i++){
if(a%i==0 && b%i==0){
gcdNum=i;
}
}
result.innerText=`${gcdNum}`;
}
function isNum(a,b){
if(!isNaN(a) && !isNaN(b)){
GCD(a,b);
}else{
result.innerText=`์ซ์์
๋ ฅ`;
}
}

โ point
์ ๋ ฅ์ฐฝ.value
6-1.๋ง์ฐ์ค ์ค๋ฒํ๋ฉด ์ด๋ฏธ์ง ๋ฐ๊พธ๊ธฐ
const imgBox=document.querySelector("#container > img");
imgBox.addEventListener("mouseover",()=>{
imgBox.src="./images/pic-1.jpg";
})
imgBox.addEventListener("mouseout",()=>{
imgBox.src="./images/pic-2.jpg"
})

โ point
mouseover
mouserout
๋ณ์.src
6-2. nav ํ ๊ธ
const btn=document.querySelector("#bttn");
const nav=document.querySelector("#nav");
btn.addEventListener("click",()=>{
btn.classList.toggle("active");
nav.classList.toggle("active");
})

โ point
toggle
7-1. ๋ช ๋จ ์์ฑํ๊ธฐ
const userName=document.querySelector("#username");
const userMajor=document.querySelector("#major");
const btn=document.querySelector("button");
const tbody=document.querySelector("#attendant > tbody");
btn.addEventListener("click",(e)=>{
e.preventDefault();
let newTr=document.createElement("tr");
let newTd1=document.createElement("td");
newTd1.innerText=`${userName.value}`;
let newTd2=document.createElement("td");
newTd2.innerText=`${userMajor.value}`;
newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
tbody.appendChild(newTr);
userName.value="";
userMajor.value="";
})

โ point
document.createElement("์์");
๋ถ๋ชจ.appendChild(์์);
๐ค 7-2.์๋ฆผํ์ ์์ฑ, 3์ด ํ ์ญ์
const notiBox=document.querySelector("#noti-box");
const btn=document.querySelector("#bttn");
btn.addEventListener("click",()=>{
const noti=document.createElement("div");
noti.classList.add("noti");
noti.innerText="์๋ฆผ ๋ด์ฉ์ด ํ์๋ฉ๋๋ค.";
notiBox.appendChild(noti);
setTimeout(()=>{
noti.remove();
},3000);
});

โ point
document.createElement("์์")
๋ถ๋ชจ.appendChild(์์);
setTimeout(()=>{
},์๊ฐ)
remove() : ์์๋ฅผ ์ญ์
-css
#noti-box {
position:fixed;
top:20px;
right:20px;
}
์ฐ์ธก ํ๋จ์ ๊ณ ์ ,fixed
๐์ฐธ๊ณ ์ฌ์ดํธ
'๊ณต๋ถ ๊ธฐ๋ก โ๏ธ > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Do-it! JS ์ค์ต ๋ณต์ต #unit 4 ๐ค (0) | 2023.04.12 |
|---|---|
| Do-it! JS ์ค์ต ๋ณต์ต #unit 3 (0) | 2023.04.10 |
| Do-it! JS ์ค์ต ๋ณต์ต #unit1 (0) | 2023.04.06 |
| JS-Do it! JS - #12์ผ (17,18) (0) | 2023.04.03 |
| JS-Do it! JS - #11์ผ (16) (0) | 2023.04.01 |