如何使用Visual Studio Code编写英语背诵单词网页的具体代码
时间: 2024-03-08 14:51:09 浏览: 63
制作一个网页背单词系统
5星 · 资源好评率100%
以下是一个简单的HTML、CSS和JavaScript代码示例,可以用来创建一个英语背诵单词网页:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>英语单词背诵网页</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>英语单词背诵</h1>
<div id="word">apple</div>
<button id="showBtn">显示翻译</button>
<button id="hideBtn">隐藏翻译</button>
<button id="nextBtn">下一个单词</button>
<div id="translation">苹果</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: #0078D7;
}
#word {
font-size: 36px;
font-weight: bold;
margin-top: 50px;
}
#translation {
display: none;
margin-top: 20px;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #0078D7;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #005A9E;
}
```
JavaScript代码:
```
let words = [
{ word: "apple", translation: "苹果" },
{ word: "banana", translation: "香蕉" },
{ word: "cherry", translation: "樱桃" },
{ word: "orange", translation: "橙子" },
{ word: "grape", translation: "葡萄" }
];
let index = 0;
let wordEl = document.getElementById("word");
let translationEl = document.getElementById("translation");
let showBtn = document.getElementById("showBtn");
let hideBtn = document.getElementById("hideBtn");
let nextBtn = document.getElementById("nextBtn");
showBtn.addEventListener("click", function() {
translationEl.style.display = "block";
});
hideBtn.addEventListener("click", function() {
translationEl.style.display = "none";
});
nextBtn.addEventListener("click", function() {
index = (index + 1) % words.length;
wordEl.textContent = words[index].word;
translationEl.textContent = words[index].translation;
translationEl.style.display = "none";
});
```
这段代码实现了一个简单的英语单词背诵网页,包括一个单词显示区域、一个翻译显示区域、三个按钮(显示翻译、隐藏翻译和下一个单词)、一个单词数组和一些事件监听器。你可以根据自己的需求进行修改和扩展。
阅读全文