蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

Markdown 测验:逐题反馈和可复查答案:本章首个独立实例

JAVASCRIPTEdge156.0.4314.58 / markdown-it15.0.3 / DOMPurify3.4.17 / KaTeX0.19.1 / Mermaid12.2.0;本地HTTP,固定输入quiz.js
quiz.js9 行
export function mountQuiz(root,questions){
 const state={index:0,choices:Array(questions.length).fill(null),confirmed:Array(questions.length).fill(false)};
 const heading=document.createElement('h2'),progress=document.createElement('p'),options=document.createElement('fieldset'),feedback=document.createElement('p'),confirm=document.createElement('button'),previous=document.createElement('button'),next=document.createElement('button'),finish=document.createElement('button'),restart=document.createElement('button'),result=document.createElement('p');
 progress.id='quiz-progress';heading.id='quiz-question';options.id='quiz-options';feedback.id='quiz-feedback';feedback.setAttribute('role','status');result.id='quiz-result';result.setAttribute('role','status');confirm.id='quiz-confirm';previous.id='quiz-previous';next.id='quiz-next';finish.id='quiz-finish';restart.id='quiz-restart';confirm.textContent='确认答案';previous.textContent='上一题';next.textContent='下一题';finish.textContent='查看结果';restart.textContent='重新开始';
 root.append(progress,heading,options,feedback,confirm,previous,next,finish,restart,result);
 function draw(){const q=questions[state.index];progress.textContent=`第${state.index+1}/${questions.length}题`;heading.textContent=q.question;options.replaceChildren();const legend=document.createElement('legend');legend.textContent='选择一个答案';options.append(legend);q.options.forEach((value,i)=>{const label=document.createElement('label'),input=document.createElement('input');input.type='radio';input.name='quiz-answer';input.value=String(i);input.checked=state.choices[state.index]===i;input.addEventListener('change',()=>{state.choices[state.index]=i;state.confirmed[state.index]=false;feedback.textContent='答案已修改,请重新确认';result.textContent=''});label.append(input,document.createTextNode(value));options.append(label)});feedback.textContent=state.confirmed[state.index]?((state.choices[state.index]===q.correct?'回答正确。':'回答有误。')+q.explanation):'请选择并确认';previous.disabled=state.index===0;next.disabled=state.index===questions.length-1;}
 confirm.onclick=()=>{if(state.choices[state.index]===null){feedback.textContent='请先选择答案';return}state.confirmed[state.index]=true;draw()};previous.onclick=()=>{if(state.index>0){state.index--;draw()}};next.onclick=()=>{if(state.index<questions.length-1){state.index++;draw()}};finish.onclick=()=>{const done=state.confirmed.filter(Boolean).length;if(done!==questions.length){result.textContent=`尚有${questions.length-done}题未确认`;return}const score=questions.reduce((sum,q,i)=>sum+(state.choices[i]===q.correct?1:0),0);result.textContent=`完成:${score}/${questions.length}`};restart.onclick=()=>{state.index=0;state.choices.fill(null);state.confirmed.fill(false);result.textContent='';draw()};draw();return{snapshot:()=>structuredClone(state)};
}
本例需在 Edge156.0.4314.58 / markdown-it15.0.3 / DOMPurify3.4.17 / KaTeX0.19.1 / Mermaid12.2.0;本地HTTP,固定输入 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Edge156.0.4314.58 / markdown-it15.0.3 / DOMPurify3.4.17 / KaTeX0.19.1 / Mermaid12.2.0;本地HTTP,固定输入

入口文件:projects/markdown-lab/src/quiz.js

预期结果与验证条件观察提示
  • 实际20题,全部正确20/20、全部错误0/20、改答19/20分别观察。
  • 漏答不完成,重复确认不多计,首尾导航正确,重启清空。

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Markdown

课程关联工程包(登记关联18章) · 含当前章节或实例 · mbskycom202611a519.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习