728x90
์๋ฐ์คํฌ๋ฆฝํธ : ํด์ฆ ํจ๊ณผ
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ค์ํ ํจ๊ณผ๋ค ์ค, ํด์ฆ๋ฅผ ๋งํ ์ ์๋ ํด์ฆ ํจ๊ณผ์ ๋๋ค. ๊ทธ ์ค์์๋, ์ฌ๋ฌ ๊ฐ์ ๋ฌธ์ ์ ์ฃผ๊ด์ ์ ๋ต์ ์ ๋ ฅํ ์ ์๋ ํด์ฆ ์ ํ์ ์์ ํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ฃผ๊ด์(์ฌ๋ฌ ๋ฌธ์ ) ์ ํ
์์๋ ์น๋์์ธ๊ธฐ๋ฅ์ฌ์ ํ๊ธฐ ์ํ ๋ฌธ์ ์ ๋๋ค. ๊ฐ ๋ฌธ์ ์ ์ ๋ต์ ๋งํ๋ฉด ๋ฌธ๊ตฌ๊ฐ ๋ํ๋๋ฉฐ ๊ฐ์์ง ์ ๋๋ฉ์ด์ ์ด ๊ธฐ๋ปํ๊ณ , ์ ๋ต์ ๋งํ์ง ๋ชปํ๋ฉด ์ฌํผํ๋ ์ ๋๋ฉ์ด์ ์ผ๋ก ์ ๋ ฅ๊ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ ๋ํ๋ฉ๋๋ค.
// ์ ํ์
const quizType = document.querySelectorAll(".quiz__type"); // ํด์ฆ ์ข
๋ฅ
const quizNumber = document.querySelectorAll(".quiz__question .number"); // ํด์ฆ ๋ฒํธ
const quizAsk = document.querySelectorAll(".quiz__question .ask"); // ํด์ฆ ์ง๋ฌธ
const quizConfirm = document.querySelectorAll(".quiz__answer .confirm"); // ์ ๋ต ํ์ธ ๋ฒํผ
const quizResult = document.querySelectorAll(".quiz__answer .result"); // ์ ๋ต ๊ฒฐ๊ณผ
const quizInput = document.querySelectorAll(".quiz__answer .input"); // ์ฌ์ฉ์ ์
๋ ฅ๊ฐ
const quizView = document.querySelectorAll(".quiz__view"); // ๊ฐ์์ง
// ๋ฌธ์ ์ ๋ณด : ์์๋ก ์ ์ธ๋ quizInfo ๋ด์ ๋ฐฐ์ด๋ก ๊ฐ ๋ฌธ์ ์ ์ ๋ณด๋ฅผ ์ ์ฅ
const quizInfo = [
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
1ํ",
answerNum: "1",
answerAsk: "๋ฏธ์ ๋์์ ๊ตฌ์ํ๋ ๋ถ๋ถ๊ณผ ๋ถ๋ถ์ ์ฌ์ด์ ์ง์ ์ผ๋ก๋ ์์ ์ผ๋ก ๋ชจ์๋๋ ์ผ ์์ด ์ง์๊ฐ ์กํ ์ด์ธ๋ฆฌ๋ ๊ฒ์?",
answerResult: "์กฐํ"
},
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
1ํ",
answerNum: "2",
answerAsk: "๋์์ธ ์๋ฆฌ ์ค ๋์ง์ ๋ถ๋ถ์ด ์กฐํฉ๋ ๋ ์ด๋ฃจ์ด์ง๋ ๊ฒ์?",
answerResult: "์ ์ฌ"
},
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
1ํ",
answerNum: "3",
answerAsk: " ์๊ด์ ํผํฉ์์ ์์ ํผํฉํ๋ฉด ํ ์๋ก ๋์์ง๋ ์์ ์์ฑ์?",
answerResult: "๋ช
๋"
},
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
1ํ",
answerNum: "4",
answerAsk: "์์ฑ๋ฅผ ๊ณผํ์ ์ผ๋ก ์ ๋ฆฌํ์ฌ ์คํํธ๋ผ์ 7์์ผ๋ก ๋ถ๋ฆฌํ ์ฌ๋์?",
answerResult: "๋ดํด"
}
]
// ๋ฌธ์ ์ถ๋ ฅ : 01. ์ผ์ผ์ด ์ถ๋ ฅ
{
// ์ถ์ ์๊ธฐ
quizType[0].textContent = quizInfo[0].answerType
quizType[1].textContent = quizInfo[1].answerType
quizType[2].textContent = quizInfo[2].answerType
quizType[3].textContent = quizInfo[3].answerType
// ๋ฌธ์ ๋ฒํธ
quizNumber[0].textContent = quizInfo[0].answerNum + "."
quizNumber[1].textContent = quizInfo[1].answerNum + "."
quizNumber[2].textContent = quizInfo[2].answerNum + "."
quizNumber[3].textContent = quizInfo[3].answerNum + "."
// ์ง๋ฌธ
quizAsk[0].textContent = quizInfo[0].answerAsk
quizAsk[1].textContent = quizInfo[1].answerAsk
quizAsk[2].textContent = quizInfo[2].answerAsk
quizAsk[3].textContent = quizInfo[3].answerAsk
// ์ ๋ต
quizResult[0].textContent = quizInfo[0].answerResult
quizResult[1].textContent = quizInfo[1].answerResult
quizResult[2].textContent = quizInfo[2].answerResult
quizResult[3].textContent = quizInfo[3].answerResult
}
// ๋ฌธ์ ์ถ๋ ฅ : 02. for๋ฌธ์ผ๋ก ์ถ๋ ฅ
for(let i=0; i<quizInfo.length; i++){
quizType[i].textContent = quizInfo[i].answerType // ์ถ์ ์๊ธฐ
quizNumber[i].textContent = quizInfo[i].answerNum + "." // ๋ฌธ์ ๋ฒํธ
quizAsk[i].textContent = quizInfo[i].answerAsk //์ง๋ฌธ
quizResult[i].textContent = quizInfo[i].answerResult //์ ๋ต
}
// ๋ฌธ์ ์ถ๋ ฅ : 03. forEach๋ฌธ์ผ๋ก ์ถ๋ ฅ
quizInfo.forEach(function(e, i){
quizType[i].textContent = quizInfo[i].answerType // ์ถ์ ์๊ธฐ
quizNumber[i].textContent = quizInfo[i].answerNum + "." // ๋ฌธ์ ๋ฒํธ
quizAsk[i].textContent = quizInfo[i].answerAsk //์ง๋ฌธ
quizResult[i].textContent = quizInfo[i].answerResult //์ ๋ต
})
// ์ ๋ต ์จ๊ธฐ๊ธฐ : 01. ์ผ์ผ์ด ์จ๊ธฐ๊ธฐ
quizResult[0].style.display = "none";
quizResult[1].style.display = "none";
quizResult[2].style.display = "none";
quizResult[3].style.display = "none";
// ์ ๋ต ์จ๊ธฐ๊ธฐ : 02. for๋ฌธ์ผ๋ก ์จ๊ธฐ๊ธฐ
for(let i=0; i<quizInfo.length; i++){
quizResult[i].style.display = "none";
}
// ์ ๋ต ์จ๊ธฐ๊ธฐ : 03. forEach๋ฌธ์ผ๋ก ์จ๊ธฐ๊ธฐ
quizInfo.forEach(function(e, i){
quizResult[i].style.display = "none";
})
// ์ ๋ต ํ์ธํ๊ธฐ
// ๋ฒํผ์ด ์ฌ๋ฌ ๊ฐ ์์ผ๋ฏ๋ก, ์๋ ค์ฃผ๊ธฐ ์ํด for๋ฌธ ํน์ forEach๋ฌธ์ ์ฌ์ฉํด์ผ ํจ
// ์๋์ฒ๋ผ alert์ ์ฌ์ฉํ์ฌ ์์
์ ์ค๋ฅ๊ฐ ์๋์ง ์ ๊ฒํ๋ ๊ฒ์ด ์ข์
quizConfirm.addEventListener("click", () => {
alert("add")
});
// forEach๋ฌธ์ ์ฌ์ฉ
quizConfirm.forEach((btn, num) => {
btn.addEventListener("click", () => {
// ์ฌ์ฉ์ ์
๋ ฅ๊ฐ
const userWord = quizInput[num].value.toLowerCase().trim();
// ์ฌ์ฉ์ ์
๋ ฅ๊ฐ๊ณผ ์ ๋ต ๋น๊ตํ๊ธฐ
if(userWord == quizInfo[num].answerResult){
// ์ ๋ต์ผ ๊ฒฝ์ฐ
quizView[num].classList.add("like"); // ๊ฐ์์ง
quizConfirm[num].style.display = "none"; // ์
๋ ฅ์ฐฝ ์ฌ๋ผ์ง๊ฒ ํ๊ธฐ
quizInput[num].style.display = "none"; // ๋ฒํผ ์ฌ๋ผ์ง๊ฒ ํ๊ธฐ
quizResult[num].style.display = "block"; // ์ ๋ต ๋ณด์ด๊ฒ ํ๊ธฐ
} else {
// ์ค๋ต์ผ ๊ฒฝ์ฐ
quizView[num].classList.add("dislike");
quizConfirm[num].style.display = "none";
quizInput[num].style.display = "none";
quizResult[num].style.display = "block";
}
})
})
728x90
'Javascript Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ์คํฌ๋ฆฝํธ ๊ฒ์ ํจ๊ณผ : includes() (4) | 2022.08.17 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ ๊ฒ์ ํจ๊ณผ : indexOf() (5) | 2022.08.17 |
์๋ฐ์คํฌ๋ฆฝํธ ํด์ฆ ํจ๊ณผ : ๊ฐ๊ด์ ์ ํ (13) | 2022.08.08 |
์๋ฐ์คํฌ๋ฆฝํธ ํด์ฆ ํจ๊ณผ : ์ฃผ๊ด์ ์ ํ (9) | 2022.08.04 |
์๋ฐ์คํฌ๋ฆฝํธ ํด์ฆ ํจ๊ณผ : ์ ๋ต ํ์ธํ๊ธฐ ์ ํ (10) | 2022.08.04 |
๋๊ธ