css小练习----提问app

效果图在这里插入图片描述
代码
1.label标签的for属性用来关联同一文档中另一元素的id属性。详见MDN

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回答问题</title>
    <style>
        *{
            box-sizing: border-box;
        }
        body{
            background-color: #b8c6db;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            font-family: 'Poppins', sans-serif;
        }
        .quiz-container{
            width: 600px;
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 0 10px #cccccc;
            overflow: hidden;
        }
        .header{
            padding: 4rem;
        }
        h2{
            padding: 1rem;
            text-align: center;
        }
        ul{
            list-style-type: none;
        }
        ul li{
            margin: 1rem 0;
        }
        ul li label{
            font-size: 1.2rem;
            cursor: pointer;
        }
        button{
            background-color: #8e44ad;
            cursor: pointer;
            border: none;
            width: 100%;
            color: white;
            padding: 1.3rem;
            font-size: 1.2rem;
        }
    </style>
</head>
<body>

    <div class="quiz-container" id="quiz">
        <div class="header">
            <h2 id="question">问题</h2>
            <ul>
                <li>
                    <input type="radio" name="choices" class="choices" id="A">
                    <label for="A" id="a_text"></label>
                </li>
                <li>
                    <input type="radio" name="choices" class="choices" id="B">
                    <label for="B" id="b_text"></label>
                </li>
                <li>
                    <input type="radio" name="choices" class="choices" id="C">
                    <label for="C" id="c_text"></label>
                </li>
                <li>
                    <input type="radio" name="choices" class="choices" id="D">
                    <label for="D" id="d_text"></label>
                </li>
            </ul>
        </div>
        <button class="submitBtn" id="submitBtn">Submit</button>
    </div>
</body>
<script>
    const data = [
        { question: 'Which language runs in a web browser?',
            A: 'Java',
            B: 'C',
            C: 'Python',
            D: 'JavaScript',
            correct: 'D'
        },
        { question: 'What does CSS stand for?',
            A: 'Central Style Sheets',
            B: 'Cascading Style Sheets',
            C: 'Cascading Simple Sheets',
            D: 'Cars SUVs Sailboats',
            correct: 'B'
        },
        {
            question: 'What does HTML stand for?',
            A: 'Hypertext Markup Language',
            B: 'Hypertext Markdown Language',
            C: 'Hyperloop Machine Language',
            D: 'Helicopters Terminals Motorboats Lamborginis',
            correct: 'A'
        },
        {
            question: 'What year was JavaScript launched?',
            A: '1996',
            B: '1995',
            C: '1994',
            D: 'none of the above',
            correct: 'B'
        }
    ]
    const quiz = document.getElementById('quiz')
    const question = document.getElementById('question')
    const choices = document.querySelectorAll('.choices')
    const submitBtn = document.getElementById('submitBtn')
    const a_text = document.getElementById('a_text')
    const b_text = document.getElementById('b_text')
    const c_text = document.getElementById('c_text')
    const d_text = document.getElementById('d_text')

    let currentQuiz = 0
    let score = 0

    loadQuestion()

    function loadQuestion() {
        deSelected()
        const currentData = data[currentQuiz]
        question.innerText = currentData.question
        a_text.innerText = currentData.A
        b_text.innerText = currentData.B
        c_text.innerText = currentData.C
        d_text.innerText = currentData.D
    }
    function deSelected() {
        choices.forEach( choice => {
            choice.checked = false
        })
    }
    function getSelect() {
        let answer
        choices.forEach( choice => {
            if (choice.checked){
                answer = choice.id
            }
        })
        return answer
    }
    submitBtn.addEventListener('click', ()=> {
        const answer = getSelect()
        if (answer){
            if (answer === data[currentQuiz].correct){
                score++
            }
            currentQuiz++
            if (currentQuiz< data.length){
                loadQuestion()
            }else {
                quiz.innerHTML = `
                    <h2>你回答对了${score}/${data.length}</h2>
                    <button οnclick="location.reload()">Reload</button>
                `
            }
        }
    })
</script>
</html>

好久好久没写小练习了,之前跟着网上视频写一个vue小项目,最近又跟着朋友改一个项目。昨天晚上心情很不好,但是今天早上好了。yes!嘿嘿~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值