运用 html+css+JavaScript 等技术并且把三种的代码分开来发给我
时间: 2024-01-14 11:03:27 浏览: 100
五子棋游戏的设计与实现毕业设计(含论文)
5星 · 资源好评率100%
好的,下面是一个简单的用 html、css 和 JavaScript 实现的静态页面网站的示例,包括首页、商品详情页、购物车页面和登录页面,并且每个页面都有至少两种动态交互效果。
先看一下 html 代码:
index.html:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>欢迎来到我们的网站!</h1>
<nav>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">商品</a></li>
<li><a href="#">购物车</a></li>
<li><a href="#">登录</a></li>
</ul>
</nav>
</header>
<main>
<h2>最新商品</h2>
<ul>
<li>
<a href="details_page.html">
<img src="product1.jpg" alt="商品1">
<p>商品1</p>
</a>
</li>
<li>
<a href="details_page.html">
<img src="product2.jpg" alt="商品2">
<p>商品2</p>
</a>
</li>
<li>
<a href="details_page.html">
<img src="product3.jpg" alt="商品3">
<p>商品3</p>
</a>
</li>
</ul>
</main>
<footer>
<p>版权所有 © 2021</p>
</footer>
</body>
</html>
```
details_page.html:
```
<!DOCTYPE html>
<html lang="en">
<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="#">商品</a></li>
<li><a href="#">购物车</a></li>
<li><a href="#">登录</a></li>
</ul>
</nav>
</header>
<main>
<h2>商品详情</h2>
<img id="product-img" src="product1.jpg" alt="商品1">
<p>商品1</p>
<p>价格:100元</p>
<form>
<label for="quantity">数量:</label>
<input type="number" id="quantity" min="1" value="1">
<button type="submit">加入购物车</button>
</form>
</main>
<footer>
<p>版权所有 © 2021</p>
</footer>
</body>
</html>
```
cart.html:
```
<!DOCTYPE html>
<html lang="en">
<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="#">商品</a></li>
<li><a href="#">购物车</a></li>
<li><a href="#">登录</a></li>
</ul>
</nav>
</header>
<main>
<h2>购物车</h2>
<ul>
<li>
<img src="product1.jpg" alt="商品1">
<p>商品1</p>
<p>价格:100元</p>
<form>
<label for="quantity1">数量:</label>
<input type="number" id="quantity1" min="1" value="1">
<button type="submit">更新</button>
</form>
<button class="remove">删除</button>
</li>
<li>
<img src="product2.jpg" alt="商品2">
<p>商品2</p>
<p>价格:200元</p>
<form>
<label for="quantity2">数量:</label>
<input type="number" id="quantity2" min="1" value="1">
<button type="submit">更新</button>
</form>
<button class="remove">删除</button>
</li>
</ul>
<p>总价:300元</p>
<button id="checkout">结算</button>
</main>
<footer>
<p>版权所有 © 2021</p>
</footer>
</body>
</html>
```
login.html:
```
<!DOCTYPE html>
<html lang="en">
<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="#">商品</a></li>
<li><a href="#">购物车</a></li>
<li><a href="#">登录</a></li>
</ul>
</nav>
</header>
<main>
<h2>登录</h2>
<form>
<label for="username">用户名:</label>
<input type="text" id="username">
<br>
<label for="password">密码:</label>
<input type="password" id="password">
<br>
<button type="submit">登录</button>
</form>
</main>
<footer>
<p>版权所有 © 2021</p>
</footer>
</body>
</html>
```
接下来是 css 代码:
```
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav li {
display: inline-block;
margin-right: 20px;
}
nav a {
color: #fff;
text-decoration: none;
}
main {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
margin-bottom: 20px;
}
img {
max-width: 200px;
height: auto;
margin-right: 20px;
vertical-align: middle;
}
form {
display: inline-block;
margin-right: 20px;
}
button {
background-color: #333;
color: #fff;
border: none;
padding: 5px 10px;
cursor: pointer;
}
.remove {
background-color: #ff0000;
color: #fff;
border: none;
padding: 5px 10px;
cursor: pointer;
}
footer {
background-color: #ccc;
padding: 20px;
text-align: center;
}
@media (max-width: 600px) {
header h1 {
font-size: 24px;
}
nav li {
display: block;
margin-bottom: 10px;
}
}
```
最后是 JavaScript 代码:
```
var productImg = document.getElementById("product-img");
var quantityInput = document.getElementById("quantity");
var removeButtons = document.getElementsByClassName("remove");
var checkoutButton = document.getElementById("checkout");
productImg.addEventListener("click", function() {
this.src = "product1_2.jpg";
});
quantityInput.addEventListener("input", function() {
if (this.value < 1) {
this.value = 1;
}
});
for (var i = 0; i < removeButtons.length; i++) {
removeButtons[i].addEventListener("click", function() {
this.parentNode.parentNode.removeChild(this.parentNode);
});
}
checkoutButton.addEventListener("click", function() {
alert("结算成功!");
});
```
在 JavaScript 中,我们使用了事件监听器来实现动态交互效果,例如点击图片放大、数量输入框限制、删除购物车商品、结算成功提示等。
阅读全文