继续扩展之前的代码,实现更多功能。
扩展元素周期表
要使元素周期表交互式,可以为每个元素添加点击事件,显示元素的详细信息。
javascript Copy code
function showPeriodicTable() {
const elements = [
{ name: '氢', symbol: 'H', atomicNumber: 1, info: '最轻的元素' },
{ name: '氦', symbol: 'He', atomicNumber: 2, info: '惰性气体' },
// 添加更多元素...
];
let html = '<h1>元素周期表</h1>';
elements.forEach((element, index) => {
html += `
<div class="element-card" onclick="showElementInfo(${index})">
<h2>${element.name} (${element.symbol})</h2>
<p>原子序数: ${element.atomicNumber}</p>
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2245%22%20fill%3D%22%23f0f0f0%22%2F%3E%3Ctext%20x%3D%2250%22%20y%3D%2250%22%20text-anchor%3D%22middle%22%20dominant-baseline%3D%22middle%22%3E${element.symbol}%3C%2Ftext%3E%3C%2Fsvg%3E"/>
</div>
`;
});
contentDiv.innerHTML = html;
}
function showElementInfo(index) {
const elements = [
{ name: '氢', symbol: 'H', atomicNumber: 1, info: '最轻的元素' },
{ name: '氦', symbol: 'He', atomicNumber: 2, info: '惰性气体' },
// 添加更多元素...
];
const element = elements[index];
let html = `
<h1>${element.name} (${element.symbol})</h1>
<p>原子序数: ${element.atomicNumber}</p>
<p>信息: ${element.info}</p>
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2245%22%20fill%3D%22%23f0f0f0%22%2F%3E%3Ctext%20x%3D%2250%22%20y%3D%2250%22%20text-anchor%3D%22middle%22%20dominant-baseline%3D%22middle%22%3E${element.symbol}%3C%2Ftext%3E%3C%2Fsvg%3E"/>
`;
contentDiv.innerHTML = html;
}
实现化合价表
化合价表可以使用表格形式展示。
javascript Copy code
function showValenceTable() {
const valences = [
{ element: 'H', valence: '+1' },
{ element: 'O', valence: '-2' },
// 添加更多元素和化合价...
];
let html = '<h1>化合价表</h1><table border="1">';
html += '<tr><th>元素</th><th>化合价</th></tr>';
valences.forEach(valence => {
html += `<tr><td>${valence.element}</td><td>${valence.valence}</td></tr>`;
});
html += '</table>';
contentDiv.innerHTML = html;
}
实现化学式表
化学式表可以按类别分类展示。
javascript Copy code
function showFormulaTable() {
const formulas = [
{ category: '单质', name: '氢', formula: 'H2' },
{ category: '氧化物', name: '二氧化碳', formula: 'CO2' },
// 添加更多化合物...
];
let html = '<h1>化学式表</h1>';
const categories = Array.from(new Set(formulas.map(f => f.category)));
categories.forEach(category => {
html += `<h2>${category}</h2>`;
formulas.filter(f => f.category === category).forEach(formula => {
html += `<p>${formula.name}: ${formula.formula}</p>`;
});
});
contentDiv.innerHTML = html;
}
实现方程式表
方程式表可以按反应类型分类展示。
javascript Copy code
function showEquationTable() {
const equations = [
{ type: '燃烧反应', equation: '2H2 + O2 → 2H2O' },
// 添加更多方程式...
];
let html = '<h1>方程式表</h1>';
const types = Array.from(new Set(equations.map(e => e.type)));
types.forEach(type => {
html += `<h2>${type}</h2>`;
equations.filter(e => e.type === type).forEach(equation => {
html += `<p>${equation.equation}</p>`;
});
});
contentDiv.innerHTML = html;
}
实现基础知识库
基础知识库可以使用图表形式展示。
javascript Copy code
function showKnowledgeBase() {
let html = '<h1>基础知识库</h1>';
// 添加基础知识内容...
html += '<h2>高锰酸钾制备氧气</h2>';
html += '<p>实验现象:...</p>';
html += '<p>实验结果:...</p>';
contentDiv.innerHTML = html;
}
实现元素记忆游戏
元素记忆游戏可以通过随机抽取元素并提供多个选项来实现。
javascript Copy code
function showElementGame() {
const elements = [
{ name: '氢', symbol: 'H' },
{ name: '氦', symbol: 'He' },
// 添加更多元素...
];
const randomElement = elements[Math.floor(Math.random() * elements.length)];
const options = [
{ name: '氢', symbol: 'H' },
{ name: '氦', symbol: 'He' },
{ name: '锂', symbol: 'Li' },
{ name: '铍', symbol: 'Be' },
];
let html = '<h1>元素记忆游戏</h1>';
html += `<p>元素符号:${randomElement.symbol}</p>`;
html += '<p>请选择正确的元素名称:</p>';
options.forEach(option => {
html += `<button onclick="checkAnswer('${option.name}', '${randomElement.name}')">${option.name}</button>`;
});
contentDiv.innerHTML = html;
}
function checkAnswer(selected, correct) {
if (selected === correct) {
alert('正确!');
} else {
alert('错误,正确答案是' + correct);
}
}
实现化学式记忆游戏
化学式记忆游戏可以通过随机抽取化学式并提供多个选项来实现。
javascript Copy code
function showFormulaGame() {
const formulas = [
{ name: '水', formula: 'H2O' },
{ name: '二氧化碳', formula: 'CO2' },
// 添加更多化合物...
];
const randomFormula = formulas[Math.floor(Math.random() * formulas.length)];
const options = [
{ name: '水', formula: 'H2O' },
{ name: '二氧化碳', formula: 'CO2' },
{ name: '甲烷', formula: 'CH4' },
{ name: '乙醇', formula: 'C2H5OH' },
];
let html = '<h1>化学式记忆游戏</h1>';
html += `<p>化学式:${randomFormula.formula}</p>`;
html += '<p>请选择正确的化学物质名称:</p>';
options.forEach(option => {
html += `<button onclick="checkFormulaAnswer('${option.name}', '${randomFormula.name}')">${option.name}</button>`;
});
contentDiv.innerHTML = html;
}
function checkFormulaAnswer(selected, correct) {
if (selected === correct) {
alert('正确!');
} else {
alert('错误,正确答案是' + correct);
}
}
实现方程式记忆游戏
方程式记忆游戏可以通过随机抽取方程式并提供多个选项来实现。
javascript Copy code
function showEquationGame() {
const equations = [
{ equation: '2H2 + O2 → 2H2O', name: '燃烧反应' },
// 添加更多方程式...
];
const randomEquation = equations[Math.floor(Math.random() * equations.length)];
const options = [
{ equation: '2H2 + O2 → 2H2O', name: '燃烧反应' },
{ equation: 'CaCO3 → CaO + CO2', name: '分解反应' },
{ equation: '2Na + Cl2 → 2NaCl', name: '化合反应' },
{ equation: 'Zn + CuSO4 → ZnSO4 + Cu', name: '置换反应' },
];
let html = '<h1>方程式记忆游戏</h1>';
html += `<p>方程式:${randomEquation.equation}</p>`;
html += '<p>请选择正确的反应类型:</p>';
options.forEach(option => {
html += `<button onclick="checkEquationAnswer('${option.name}', '${randomEquation.name}')">${option.name}</button>`;
});
contentDiv.innerHTML = html;
}
function checkEquationAnswer(selected, correct) {
if (selected === correct) {
alert('正确!');
} else {
alert('错误,正确答案是' + correct);
}
}
实现综合题库
综合题库可以通过随机抽取题目并提供多个选项来实现。
javascript Copy code
function showComprehensiveLibrary() {
const questions = [
{ question: '什么是化合物?', options: ['单质', '化合物', '混合物'], correct: '化合物' },
// 添加更多题目...
];
const randomQuestion = questions[Math.floor(Math.random() * questions.length)];
let html = '<h1>综合题库</h1>';
html += `<p>问题:${randomQuestion.question}</p>`;
html += '<p>请选择正确的答案:</p>';
randomQuestion.options.forEach(option => {
html += `<button onclick="checkComprehensiveAnswer('${option}', '${randomQuestion.correct}')">${option}</button>`;
});
contentDiv.innerHTML = html;
}
function checkComprehensiveAnswer(selected, correct) {
if (selected === correct) {
alert('正确!');
} else {
alert('错误,正确答案是' + correct);
}
}
实现得分统计
得分统计可以通过记录每个游戏板块的正确答案数量来实现。
javascript Copy code
let scores = {
elementGame: 0,
formulaGame: 0,
equationGame: 0,
comprehensiveLibrary: 0,
};
function updateScore(gameType) {
scores[gameType]++;
}
function showScoreStatistics() {
let html = '<h1>得分统计</h1>';
Object.keys(scores).forEach(gameType => {
html += `<p>${gameType}: ${scores[gameType]} 分</p>`;
});
contentDiv.innerHTML = html;
}
最终代码
将所有功能整合到一起,确保每个功能都能正常工作。注意,完整的代码需要根据具体需求进行调整和优化。