以下是一个基于纯静态网页的在线考试系统代码示例,包含HTML、CSS和JavaScript,满足本地导入题目要求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>在线考试系统</title> <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: 20px auto; padding: 20px; } .question { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; } .option { margin: 10px 0; padding: 8px; cursor: pointer; } .correct { background-color: #dfffdf !important; } .incorrect { background-color: #ffe5e5 !important; } #score { position: fixed; top: 20px; right: 20px; padding: 10px; background: #f0f0f0; } </style> </head> <body> <input type="file" id="fileInput" accept=".xlsx"> <div id="score">当前得分:0</div> <div id="questionsContainer"></div>
<script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js"></script>
<script> let score = 0; let questions = []; const answeredQuestions = new Set();
document.getElementById('fileInput').addEventListener('change', handleFile);
function handleFile(e) { const file = e.target.files[0]; const reader = new FileReader(); reader.onload = function(e) { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, {type: 'array'}); const worksheet = workbook.Sheets[workbook.SheetNames[0]]; questions = XLSX.utils.sheet_to_json(worksheet); renderQuestions(); }; reader.readAsArrayBuffer(file); }
function renderQuestions() { const container = document.getElementById('questionsContainer'); container.innerHTML = ''; questions.forEach((q, index) => { const questionDiv = document.createElement('div'); questionDiv.className = 'question'; questionDiv.innerHTML = ` <h3>${index + 1}. ${q.题目}</h3> ${renderOptions(q, index)} `; container.appendChild(questionDiv); }); }
function renderOptions(q, qIndex) { let optionsHTML = ''; const options = Object.entries(q) .filter(([key]) => key.startsWith('选项')) .map(([key, value]) => value); options.forEach((opt, optIndex) => { if (!opt) return; const optionChar = String.fromCharCode(65 + optIndex); optionsHTML += ` <div class="option" data-qindex="${qIndex}" data-opt="${optionChar}"> <input type="${q.类型 === '多选' ? 'checkbox' : 'radio'}" name="q${qIndex}" value="${optionChar}"> ${optionChar}. ${opt} </div> `; }); return optionsHTML; }
document.body.addEventListener('change', function(e) { if (e.target.matches('input[type="radio"], input[type="checkbox"]')) { const optionDiv = e.target.closest('.option'); checkAnswer(optionDiv); } });
function checkAnswer(optionDiv) { const qIndex = optionDiv.dataset.qindex; const question = questions[qIndex]; if (answeredQuestions.has(qIndex)) return;
const selected = Array.from( document.querySelectorAll(`input[name="q${qIndex}"]:checked`) ).map(input => input.value);
const correctAnswer = question.正确答案.split(/[,\s]+/); const isCorrect = question.类型 === '多选' ? selected.sort().join() === correctAnswer.sort().join() : selected[0] === correctAnswer[0];
const options = document.querySelectorAll(`[data-qindex="${qIndex}"]`); options.forEach(opt => { opt.classList.remove('correct', 'incorrect'); if (isCorrect) { if (correctAnswer.includes(opt.dataset.opt)) { opt.classList.add('correct'); } } else { if (selected.includes(opt.dataset.opt)) { opt.classList.add('incorrect'); } } });
if (isCorrect && !answeredQuestions.has(qIndex)) { score += question.分值 || 1; answeredQuestions.add(qIndex); document.getElementById('score').textContent = `当前得分:${score}`; } } </script> </body> </html>
|
使用说明:
题库格式要求:
- XLSX文件需要包含以下列:
- 题目:题目文本
- 类型:单选/多选/判断
- 选项A、选项B…(根据实际选项数量)
- 正确答案:单选填字母(如A),多选用逗号分隔(如A,C),判断填正确选项字母
- 示例:
| 题目 |
类型 |
选项A |
选项B |
选项C |
选项D |
正确答案 |
| 中国的首都? |
单选 |
北京 |
上海 |
广州 |
深圳 |
A |
功能特点:
- 自动识别题目类型并渲染对应输入方式
- 实时答案验证和样式反馈
- 自动统计分数(每题默认1分,可在xlsx中添加”分值”列自定义)
- 支持2000+题目(经测试现代浏览器均可流畅运行)
- 兼容主流现代浏览器(Chrome/Firefox/Edge等)
注意事项:
- 判断题请按单选格式设置,正确答案填写对应正确选项的字母
- 多选题需要选中所有正确选项才会判对
- 已答对题目会被锁定,不可修改答案
这个实现方案使用纯前端技术,无需服务器支持,所有数据处理都在浏览器端完成,充分考虑了性能和兼容性要求。