用代码编译:运用 html+css+JavaScript 等技术,开发静态页面网站。 要求如下: 1、网站至少包含 4 个页面,如电商类网站,包含首页(index.html)、商品详情页 (details_page.html)、购物车页面(cart.html),登陆页面(login.html) 2、每个页面至少包含两种 js 实现的动态交互效果
时间: 2024-03-26 16:34:27 浏览: 70
很好,以下是一个简单的示例代码,可以用来实现一个符合要求的静态页面网站:
index.html:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<header>
<h1>欢迎来到我的网站!</h1>
<nav>
<ul>
<li><a href="index.html">首页</a></li>
<li><a href="details_page.html">商品详情页</a></li>
<li><a href="cart.html">购物车页面</a></li>
<li><a href="login.html">登陆页面</a></li>
</ul>
</nav>
</header>
<section>
<h2>最新商品</h2>
<ul>
<li><a href="#">商品1</a></li>
<li><a href="#">商品2</a></li>
<li><a href="#">商品3</a></li>
<li><a href="#">商品4</a></li>
</ul>
</section>
<footer>
<p>© 2021 My Website</p>
</footer>
</body>
</html>
```
details_page.html:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品详情页</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<header>
<h1>欢迎来到我的网站!</h1>
<nav>
<ul>
<li><a href="index.html">首页</a></li>
<li><a href="details_page.html">商品详情页</a></li>
<li><a href="cart.html">购物车页面</a></li>
<li><a href="login.html">登陆页面</a></li>
</ul>
</nav>
</header>
<section>
<h2>商品详情</h2>
<p>这里是商品1的详情信息。</p>
<button id="add-to-cart">加入购物车</button>
</section>
<footer>
<p>© 2021 My Website</p>
</footer>
</body>
</html>
```
cart.html:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车页面</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<header>
<h1>欢迎来到我的网站!</h1>
<nav>
<ul>
<li><a href="index.html">首页</a></li>
<li><a href="details_page.html">商品详情页</a></li>
<li><a href="cart.html">购物车页面</a></li>
<li><a href="login.html">登陆页面</a></li>
</ul>
</nav>
</header>
<section>
<h2>您的购物车</h2>
<ul id="cart-items">
<li>商品1</li>
<li>商品2</li>
</ul>
<button id="clear-cart">清空购物车</button>
</section>
<footer>
<p>© 2021 My Website</p>
</footer>
</body>
</html>
```
login.html:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<header>
<h1>欢迎来到我的网站!</h1>
<nav>
<ul>
<li><a href="index.html">首页</a></li>
<li><a href="details_page.html">商品详情页</a></li>
<li><a href="cart.html">购物车页面</a></li>
<li><a href="login.html">登陆页面</a></li>
</ul>
</nav>
</header>
<section>
<h2>登陆</h2>
<form>
<label for="username">用户名:</label>
<input type="text" id="username"><br>
<label for="password">密码:</label>
<input type="password" id="password"><br>
<button id="login-button">登陆</button>
</form>
</section>
<footer>
<p>© 2021 My Website</p>
</footer>
</body>
</html>
```
style.css:
```css
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
header {
background-color: #333;
color: #fff;
padding: 10px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav li {
display: inline-block;
margin-right: 10px;
}
nav a {
color: #fff;
text-decoration: none;
}
section {
margin: 20px;
}
h2 {
font-size: 24px;
margin-bottom: 10px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
margin-bottom: 5px;
}
button {
background-color: #333;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
}
form label {
display: inline-block;
width: 100px;
}
form input {
padding: 5px;
margin-bottom: 10px;
width: 200px;
}
footer {
background-color: #333;
color: #fff;
padding: 10px;
text-align: center;
}
```
script.js:
```js
// 在商品详情页中,点击“加入购物车”按钮时,将商品添加到购物车页面中
var addToCartButton = document.getElementById('add-to-cart');
if (addToCartButton) {
addToCartButton.addEventListener('click', function() {
var cartItems = document.getElementById('cart-items');
var newItem = document.createElement('li');
newItem.textContent = '商品1(1)';
cartItems.appendChild(newItem);
});
}
// 在购物车页面中,点击“清空购物车”按钮时,将购物车清空
var clearCartButton = document.getElementById('clear-cart');
if (clearCartButton) {
clearCartButton.addEventListener('click', function() {
var cartItems = document.getElementById('cart-items');
while (cartItems.firstChild) {
cartItems.removeChild(cartItems.firstChild);
}
});
}
// 在登陆页面中,点击“登陆”按钮时,验证用户名和密码是否正确
var loginButton = document.getElementById('login-button');
if (loginButton) {
loginButton.addEventListener('click', function(event) {
event.preventDefault();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
if (username === 'admin' && password === '123456') {
alert('登陆成功!');
} else {
alert('用户名或密码错误,请重试。');
}
});
}
```
这是一个简单的示例,可以根据实际需求进行修改和扩展。希望对你有所帮助!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)