๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

ํ€ด์ฆˆ ๋ฌธ์ œ ๋งŒ๋“ค๊ธฐ04

by _ํ† ๋งคํ†  2022. 8. 8.
728x90

ํ€ด์ฆˆ ๊ฐ๊ด€์‹ ์œ ํ˜•

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ ํ•„๊ธฐ๋ฌธ์ œ๋ฅผ ๊ฐ๊ด€์‹ ์œ ํ˜• ํ€ด์ฆˆ๋กœ ์—ฌ๋Ÿฌ๊ฐœ ์ œ์ž‘ํ•ด๋ณด๊ธฐ
ํ•˜๋‚˜์˜ ๊ฐ๊ด€์‹ ๋ฌธ์ œ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด๋ณด์ž!


#1. ์„ ํƒ์ž ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

์ƒ์ˆ˜์—๊ฒŒ ํ•ด๋‹น๋ช…์นญ(ex: quizType)์„ ์„ ์–ธํ•ด์ฃผ๊ณ  ์ฟผ๋ฆฌ์…€๋ž™ํ„ฐ(ํƒœ๊ทธ, ์•„์ด๋””, ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ)๋กœ ์„ ํƒํ•œ ์š”์†Œ๋ฅผ ์ฐพ์•„์„œ ๋„ฃ์–ด๋ณด์ž!

 //์„ ํƒ์ž
const quizType = document.querySelector(".quiz__type"); //ํ€ด์ฆˆ์ข…๋ฅ˜
const quizNumber = document.querySelector(".quiz__question .number"); //ํ€ด์ฆˆ ๋ฒˆํ˜ธ
const quizAsk = document.querySelector(".quiz__question .ask"); //ํ€ด์ฆˆ ์งˆ๋ฌธ
const quizConfirm = document.querySelector(".quiz__answer .confirm"); //ํ€ด์ฆˆ ํ™•์ธ ๋ฒ„ํŠผ
const quizResult = document.querySelector(".quiz__answer .result"); //์ •๋‹ต ๊ฒฐ๊ณผ
const quizView = document.querySelector(".quiz__view"); //๊ฐ•์•„์ง€
const quizSelects = document.querySelector(".quiz__selects"); //๊ฐ๊ด€์‹ ๋ณด๊ธฐ
const quizChoice = quizSelects.querySelectorAll(".choice"); //๊ฐ๊ด€์‹ ๋ณด๊ธฐ
const quizSelect = quizSelects.querySelectorAll(".select"); //๊ฐ๊ด€์‹ ์„ ํƒ

//quizSelects.querySelectorAll(".select") = ํ€ด์ฆˆ ์…€๋ ‰ํ„ฐ ์•ˆ์— ์…€๋ ‰ํ„ฐ๋ฅผ ์ฐพ๊ฒŒ ํ•ด์ค€๋‹ค.
//choice์™€ select๋Š” ๋ณต์ˆ˜๋กœ ๊ตฌํ˜„๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— querySelectorAll์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋‘ ์„ ํƒํ•ด์ค€๋‹ค.

=> document ๋Œ€์‹  quizSelects๋ฅผ ์„ ์–ธํ•œ ์ด์œ ๋Š” ๋งŒ์•ฝ ๋ฌธ์ œ๊ฐ€ 100๊ฐœ ์ด์ƒ์ด ๋˜๋ฉด ์„ ํƒ๊ณผ ๋ณด๊ธฐ๋Š” 400๊ฐœ ์ด์ƒ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— document์—์„œ ์ฐพ๋Š” ๊ฒƒ ๋ณด๋‹จ ์š”์†Œ๊ฐ€ ์†Œ์†๋˜์–ด ์žˆ๋Š” ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ์จ์ฃผ์–ด์•ผ ๋” ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

#2. ๋ฌธ์ œ ์ •๋ณด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

๋ฐฐ์—ด ์•ˆ ๊ฐ์ฒด๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์—ฌ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฌธ์ œ์ •๋ณด(๋ฐ์ดํ„ฐ)๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅ์‹œํ‚จ๋‹ค.

//๋ฌธ์ œ์ •๋ณด
const quizInfo = [
    {
        answerType : "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2010๋…„ 01ํšŒ",
        answerNum : "1",
        answerAsk : "๋ถ„์‹ , ํ™”์‹ ์ด๋ผ๋Š” ์˜๋ฏธ๋กœ ์ฑ„ํŒ…, ์˜จ๋ผ์ธ ๊ฒŒ์ž„ ๋“ฑ ๋„คํฌ์›Œํฌ ํ™˜๊ฒฝ์—์„œ ์ž์‹ ์„ ๋Œ€์‹ ํ•˜์—ฌ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์— ์ฐธ์—ฌํ•˜๋Š” ๊ฐ€์ƒ์˜ ๊ทธ๋ฆผ ๋˜๋Š” ์•„์ด์ฝ˜์„ ๋œปํ•˜๋Š” ์ธํ„ฐ๋„ท ์šฉ์–ด๋Š” ๋ฌด์—‡์ธ๊ฐ€?",
        answerChoice : ["์•„๋ฐ”ํƒ€", "์ฟ ํ‚ค", "ํฌํ„ธ", "ํ—ˆ๋ธŒ"],
        answerResult : "1",
        answerEx : "์ •๋‹ต์€ ์•„๋ฐ”ํƒ€์ด๋‹ค. ๋ถ„์‹ , ํ™”์‹ ์ด๋ผ๋Š” ์˜๋ฏธ๋กœ ์ฑ„ํŒ…, ์˜จ๋ผ์ธ ๊ฒŒ์ž„ ๋“ฑ ๋„คํฌ์›Œํฌ ํ™˜๊ฒฝ์—์„œ ์ž์‹ ์„ ๋Œ€์‹ ํ•˜์—ฌ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์— ์ฐธ์—ฌํ•˜๋Š” ๊ฐ€์ƒ์˜ ๊ทธ๋ฆผ ๋˜๋Š” ์•„์ด์ฝ˜์„ ๋œปํ•˜๋Š” ์ธํ„ฐ๋„ท ์šฉ์–ด๋ฅผ ์•„๋ฐ”ํƒ€๋ผ๊ณ  ํ•œ๋‹ค."
    }
]

#3. ๋ฌธ์ œ ์ถœ๋ ฅํ•˜๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

textContent๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ€ด์ฆˆ ๋ฌธ์ œ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์ž. ์ด๋•Œ ๊ฐ๊ด€์‹ ๋ณด๊ธฐ์™€ ์„ ํƒ์นธ์€ ๋ณต์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋”ฐ๋กœ ์ถœ๋ ฅํ•ด๋ณด์ž!

function updateQuiz(){
    //ํ€ด์ฆˆ ์ข…๋ฅ˜, ๋ฒˆํ˜ธ, ์งˆ๋ฌธ, ์ •๋‹ต(ํ•ด์„ค) ์ถœ๋ ฅ
    quizType.textContent = quizInfo[0].answerType;
    quizNumber.textContent = quizInfo[0].answerNum + ". ";
    quizAsk.textContent = quizInfo[0].answerAsk;
    quizResult.textContent = quizInfo[0].answerEx; //์ •๋‹ต ๋ž€์— ํ•ด์„ค์„ ๋„ฃ์–ด์„œ ๋‹ต๋Œ€์‹  ํ•ด์„ค์ด ๊ตฌํ˜„๋˜๋„๋ก ํ•œ๋‹ค.



    //๊ฐ๊ด€์‹ ๋ณด๊ธฐ ์ถœ๋ ฅ
    
    //for๋ฌธ
     for( let i=0; i<4; i++){
         quizChoice[i].textContent = quizInfo[0].answerChoice[i];
     }

    //forEach๋ฌธ
    quizChoice.forEach(function (e, i){
        quizChoice[i].textContent = quizInfo[0].answerChoice[i];
    });



    // ํ•ด์„ค ์ˆจ๊ธฐ๊ธฐ
    quizResult.style.display = "none";
    
}
updateQuiz(); // = ํ•จ์ˆ˜์‹คํ–‰๋ฌธ

function ํ•จ์ˆ˜๋ช…์นญ(){};
์œ„์™€ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ•ด๋‹นํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ(๋ฐ˜๋ณต๋ฌธ, ์ถœ๋ ฅ ๋“ฑ)๋ฅผ ๋ฌถ์–ด์ฃผ์–ด์„œ ์ถฉ๋Œ ๋ฐ ์˜ค๋ฅ˜์˜ ๊ฐ€๋Šฅ์„ฑ์„ ์—†์• ์ค€๋‹ค.
ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ผญ ๊ด„ํ˜ธ๋ฅผ ๋‹ซ์€ ๋’ค ํ•จ์ˆ˜ ์‹คํ–‰๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ •์ƒ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.

<for๋ฌธ์œผ๋กœ ๊ฐ๊ด€์‹ ๋ณด๊ธฐ ์ถœ๋ ฅ>

i๊ฐ€ 0์ผ ๋•Œ i๊ฐ€ 4๋ณด๋‹ค ์ž‘์€ 3๊นŒ์ง€ ๋ฐ˜๋ณต์‹œํ‚จ๋‹ค. ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๊ฐ๊ฐ์˜ quizChoice์— answerChoice๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋Œ€์ž…ํ•˜์—ฌ ๊ตฌํ˜„์‹œํ‚จ๋‹ค.

for( let i=0; i<4; i++){
    quizChoice[i].textContent = quizInfo[0].answerChoice[i];
}
for๋ฌธ๊ณผ length
ํ˜„ ๋ฐ์ดํ„ฐ๋Š” ๋‹จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— quizInfo.length๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 1๋ฒˆ๋ฐ–์— ๋ฐ˜๋ณต์ด ๋˜์ง€ ์•Š์•„์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค. ๋•Œ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด์— ๋งž๊ฒŒ ์ธ๋ฑ์Šค๋กœ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ์ œ๋Œ€๋กœ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
๋งŒ์•ฝ length๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด i < quizInfo[0].answerChoice[i].length; ๋กœ ํ‘œํ˜„ํ•˜๋ฉด answerChoice์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๋Š” ๋‚˜์ง€ ์•Š๋Š”๋‹ค.

<forEach๋ฌธ์œผ๋กœ ๊ฐ๊ด€์‹ ๋ณด๊ธฐ ์ถœ๋ ฅ>

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ๊ด€์‹ ๋ณด๊ธฐ ์ถœ๋ ฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋˜์–ด์•ผ ํ•˜๋Š” quizChoice๋กœ forEach๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.
4๋ฒˆ์„ ๋ฐ˜๋ณตํ•˜์—ฌ quizChoice์— answerChoice๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋Œ€์ž…ํ•˜์—ฌ ๊ตฌํ˜„์‹œํ‚จ๋‹ค.

quizChoice.forEach(function (e, i){
    quizChoice[i].textContent = quizInfo[0].answerChoice[i];
});

#4. ์ •๋‹ต ํ™•์ธํ•˜๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ for๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•œ ์ •๋‹ต ํ™•์ธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ฌถ์–ด๋ณด์ž.
๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ ํ•จ์ˆ˜ ์‹คํ–‰๋ฌธ์—์„œ addEventListener๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„๋˜๋Š” ์ด๋ฒคํŠธ๋กœ ์ •๋‹ต์„ ํ™•์ธํ•ด๋ณด์ž!

//์ •๋‹ต ํ™•์ธ
function answerQuiz(){
    //์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ์ •๋‹ต์ด ๋ฌธ์ œ์ •๋‹ต๊ณผ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ธฐ (์‚ฌ์šฉ์ž๋‹ต == ๋ฌธ์ œ ์ •๋‹ต)
    //์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ input์—์„œ css ์†์„ฑ์ธ checked ์Šคํƒ€์ผ์ด ๋‚˜ํƒ€๋‚ ๋•Œ ์ •๋‹ต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

    for( let i=0; i<quizSelect.length; i++){
        if(quizSelect[i].checked == true){ //๋ณด๊ธฐ์— ์ฒดํฌ๊ฐ€ ๋˜์–ด์žˆ๋Š” ์ƒํƒœ์ผ ๊ฒฝ์šฐ 
            //์ฒดํฌ๋œ ๋ฒˆํ˜ธ์™€ ์ •๋‹ต ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ true
            if(quizSelect[i].value == quizInfo[0].answerResult){
                // alert("์ •๋‹ต");
                quizView.classList.add("like");
            } else {
                // alert("์˜ค๋‹ต");
                quizView.classList.add("dislike");
                quizResult.style.display = "block";
                quizConfirm.style.display = "none";
            }
        } else {}
    }
}
quizConfirm.addEventListener("click", answerQuiz); //ํ•จ์ˆ˜์‹คํ–‰๋ฌธ

//๊ธฐ์กด ํ•จ์ˆ˜์‹คํ–‰๋ฌธ์ธ answerQuiz();๋Œ€์‹  addEventListener๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

for๋ฌธ ์ •๋ฆฌ
i๊ฐ€ 0์ผ ๋•Œ, quizSelect์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜ํ™˜ํ•ด๋ผ.
๊ทธ๋ฆฌ๊ณ  ์ด 4๊ฐœ์˜ quizSelect๋“ค ์ค‘ ํ•œ๊ฐ€์ง€๋ฅผ ์ฒดํฌ๋ฅผ ํ–ˆ์„ ๋•Œ true์ผ ๊ฒฝ์šฐ, ๋˜ ๋งŒ์•ฝ ๊ทธ ์ฒดํฌํ•œ ๊ฐ’์ด ์ •๋‹ต ๊ฐ’๊ณผ ๊ฐ™์„ ๋•Œ classList.add()๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ.
false์ผ ๊ฒฝ์šฐ์—๋„ ํ•ด๋‹นํ•˜๋Š” classList.add()๋ฅผ ๊ตฌํ˜„ํ•ด๋ผ. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ชจ๋“  ์ด๋ฒคํŠธ๋Š” quizConfirm์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋„๋ก ํ•˜์—ฌ๋ผ.


!๊ธฐ์–ตํ•ด์•ผ ํ•  ๋ฌธ๋ฒ•!

ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ์Šคํฌ๋ฆฝํŠธ ๋ฌถ๊ธฐ
์—ฌ๋Ÿฌ๊ฐœ์˜ ์Šคํฌ๋ฆฝํŠธ(๋ฐ์ดํ„ฐ ์ €์žฅ๊ณผ ์ถœ๋ ฅ, ๋ฐ˜๋ณต๋ฌธ ๋“ฑ)๊ฐ€ ์ถฉ๋Œ๋˜์ง€ ์•Š๋„๋ก ํ•œ ๋ฉ์–ด๋ฆฌ ์”ฉ ํ•จ์ˆ˜๋กœ ๋ฌถ์–ด์ค€๋‹ค.

function answerQuiz(){
    for(){}
};
answerQuiz();

if๋ฌธ์—์„œ css์†์„ฑ ๊ฐ€์ ธ์˜ค๊ธฐ
css์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” .ํด๋ž˜์Šค ๋ช…์„ ์„ ์–ธํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

if(quizSelect[i].checked == true){}

๊ฒฐ๊ณผ๋ณด๊ธฐ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css