怎么用html,css,js写一个性格测试网页
时间: 2024-03-14 19:43:58 浏览: 24
好的,下面是一个简单的性格测试网页的HTML,CSS和JavaScript代码示例:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>性格测试</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>性格测试</h1>
<p>请选择以下选项:</p>
<form id="quiz-form">
<div class="question">
<p>1. 你喜欢在人多的场合中吗?</p>
<label><input type="radio" name="q1" value="a">是</label>
<label><input type="radio" name="q1" value="b">否</label>
</div>
<div class="question">
<p>2. 你更喜欢独处还是与他人相处?</p>
<label><input type="radio" name="q2" value="a">独处</label>
<label><input type="radio" name="q2" value="b">与他人相处</label>
</div>
<div class="question">
<p>3. 你喜欢计划好你的时间吗?</p>
<label><input type="radio" name="q3" value="a">是</label>
<label><input type="radio" name="q3" value="b">否</label>
</div>
<button type="submit" class="submit-btn">提交</button>
</form>
<div id="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0px 0px 10px #ccc;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.question {
margin-bottom: 10px;
}
.submit-btn {
margin-top: 20px;
background-color: #4CAF50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
#result {
margin-top: 20px;
text-align: center;
font-size: 24px;
font-weight: bold;
}
```
JavaScript代码:
```javascript
const quizForm = document.getElementById('quiz-form');
const resultDiv = document.getElementById('result');
const quizData = [
{
question: "你喜欢在人多的场合中吗?",
a: "是",
b: "否",
},
{
question: "你更喜欢独处还是与他人相处?",
a: "独处",
b: "与他人相处",
},
{
question: "你喜欢计划好你的时间吗?",
a: "是",
b: "否",
},
];
function showResults() {
let score = 0;
const answers = quizForm.querySelectorAll('input:checked');
answers.forEach(answer => {
if (answer.value === 'a') {
score++;
}
});
resultDiv.innerHTML = `你的得分为:${score}`;
}
function loadQuiz() {
const quizHTML = quizData.map((data, index) => {
return `
<div class="question">
<p>${index + 1}. ${data.question}</p>
<label><input type="radio" name="q${index+1}" value="a">${data.a}</label>
<label><input type="radio" name="q${index+1}" value="b">${data.b}</label>
</div>
`;
}).join('');
quizForm.innerHTML = quizHTML;
}
loadQuiz();
quizForm.addEventListener('submit', (event) => {
event.preventDefault();
showResults();
});
```
以上代码实现了一个简单的性格测试,包括三个问题和一个提交按钮,用户提交后会得到一个得分。你可以根据你的需求进行修改和扩展。