You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML/JavaScript Trivia页面点击按钮无颜色反馈问题求助

问题修复方案

你的代码存在三个核心问题,导致按钮点击无响应:

  1. DOM加载时机错误:JS代码放在<head>标签中,浏览器解析到这段代码时,<body>内的按钮元素还未渲染完成,document.querySelector无法找到目标按钮,事件绑定失败。
  2. 模板字符串语法错误:check函数中document.querySelector('#${id}')使用单引号包裹,模板字符串必须用反引号()包裹,否则${id}`不会被解析为实际传入的按钮ID,导致无法选中对应按钮。
  3. 变量未声明:button变量未用let/const声明,会意外成为全局变量,不符合编码规范且可能引发潜在问题。

修复后的完整代码

HTML部分(调整脚本位置+修正语法)

<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap" rel="stylesheet">
        <link href="styles.css" rel="stylesheet">
        <title>Trivia!</title>
    </head>
    <body>
        <div class="header">
            <h1>Trivia!</h1>
        </div>
        <div class="container">
            <div class="section">
                <h2> Part 1: Multiple Choice </h2>
                <hr>
                <h3>
                    What does the Mushroom tell the other Mushroom when they're the only Mushrooms in their world?
                </h3>
                <button id="1"> I'm a Fun Guy!</button>
                <button id="2">There's 2 mushroom?</button>
                <button id="3"> Let me trufle over to you.</button>
                <button id="4"> "Sigh". Lets wallop on the pizza-ria...</button>
            </div>
            <div class="section">
                <h2>Part 2: Free Response</h2>
                <hr>
            </div>
        </div>
        <!-- 将脚本移到body末尾,确保DOM已完全加载 -->
        <script>
            function check(id){
                // 使用反引号包裹模板字符串,声明button变量
                const button = document.querySelector(`#${id}`);
                if (id === '2') {
                    button.style.backgroundColor = 'green';
                } else {
                    button.style.backgroundColor = 'red';
                }
            }
            // 为每个按钮绑定点击事件
            document.querySelector('#1').addEventListener('click', function(){
                check('1');
            });
            document.querySelector('#2').addEventListener('click', function(){
                check('2');
            });
            document.querySelector('#3').addEventListener('click', function(){
                check('3');
            });
            document.querySelector('#4').addEventListener('click', function(){
                check('4');
            });
        </script>
    </body>
</html>

CSS部分(无需修改,保留原代码即可)

body {
    background-color: #fff;
    color: #212529;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    margin: 0;
    text-align: left;
}

.container {
    margin-left: auto;
    margin-right: auto;
    padding-left: 15px;
    padding-right: 15px;
}

.header {
    background-color: #477bff;
    color: #fff;
    margin-bottom: 2rem;
    padding: 2rem 1rem;
    text-align: center;
}

.section {
    padding: 0.5rem 2rem 1rem 2rem;
}

.section:hover {
    background-color: #f5f5f5;
    color: #477bff;
    transition: color 10s ease-in-out, background-color 0.15s ease-in-out;
}

h1 {
    font-family: 'Montserrat', sans-serif;
    font-size: 48px;
}

button, input[type="submit"] {
    background-color: #d9edff;
    border: 1px solid transparent;
    border-radius: 0.25rem;
    font-size: 0.95rem;
    font-weight: 400;
    line-height: 1.5;
    padding: 0.375rem 0.75rem;
    text-align: center;
    transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    vertical-align: middle;
}


input[type="text"] {
    line-height: 1.8;
    width: 25%;
}

input[type="text"]:hover {
    background-color: #f5f5f5;
    transition: color 2s ease-in-out, background-color 0.15s ease-in-out;
}

额外优化建议

可以用事件委托简化代码,无需给每个按钮单独绑定事件:

// 替换原事件绑定代码
document.querySelector('.section').addEventListener('click', function(e) {
    if (e.target.tagName === 'BUTTON') {
        check(e.target.id);
    }
});

这样后续新增按钮时,无需手动重复绑定事件,代码更简洁易维护。

内容的提问来源于stack exchange,提问作者Harrisino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:53:13