返回模板
Interactive multiple-choice quiz
A self-contained HTML quiz with instant answer scoring and replay support for classrooms.
完整源码
1 行 · 1.7 KB<style>*{box-sizing:border-box}body{margin:0;font-family:Inter,system-ui,sans-serif;background:#f6f7fb;color:#172033}button,a{font:inherit}.wrap{width:min(960px,calc(100% - 32px));margin:auto}</style><style>main{min-height:100vh;display:grid;place-items:center;padding:24px}.quiz{width:min(520px,100%);background:#fff;border:1px solid #e2e8f0;border-radius:18px;padding:28px}.opt{display:block;width:100%;text-align:left;padding:12px 14px;margin:10px 0;border:1px solid #cbd5e1;border-radius:10px;background:#fff;cursor:pointer}.opt:hover{border-color:#4f46e5}.msg{margin-top:14px;font-weight:600}</style><main><div class="quiz"><p id="step" style="color:#64748b;margin:0 0 8px"></p><h1 id="q" style="font-size:22px;margin:0 0 14px"></h1><div id="opts"></div><p id="msg" class="msg"></p></div></main><script>const data=[{q:"Which HTML attribute isolates an iframe from parent storage?",opts:["sandbox without allow-same-origin","loading=lazy","referrerpolicy=no-referrer"],ans:0},{q:"Which CSS unit scales relative to the root font size?",opts:["px","rem","vh"],ans:1}];let idx=0,score=0;function show(){const item=data[idx];document.getElementById("step").textContent="Question "+(idx+1)+" of "+data.length;document.getElementById("q").textContent=item.q;document.getElementById("msg").textContent="";const box=document.getElementById("opts");box.innerHTML="";item.opts.forEach((o,n)=>{const b=document.createElement("button");b.className="opt";b.textContent=o;b.onclick=()=>{if(n===item.ans)score++;idx++;if(idx<data.length)show();else{box.innerHTML="";document.getElementById("q").textContent="Completed!";document.getElementById("msg").textContent="Score: "+score+" / "+data.length}};box.appendChild(b)})}show()</script>适用场景
适合课堂随堂测验、入职小测试或技术文章内嵌互动练习的单文件答题模板。
布局原理
一次聚焦展示一道题和全宽选项按钮,答题完毕后自动汇总得分。
如何定制
可在题目数据中增加解析说明和「再测一次」按钮;注意不要把需要严格防作弊的正式考试答案放进前端脚本。
发布前检查
用纯键盘完整走一遍作答流程,并检查长题干在手机上的换行表现。