
在多人共同消费场景中,费用分摊是一个常见需求。本教程将引导您开发一个基于web的简易应用,旨在实现以下核心功能:
首先,我们需要搭建应用的HTML骨架,定义输入控件、操作按钮以及用于显示结果的区域。以下是完整的HTML代码,其中包含了一些基础的CSS样式以提升可读性。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>费用分摊应用</title>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px; background-color: #f4f7f6; color: #333; }
h1 { color: #2c3e50; text-align: center; margin-bottom: 30px; }
p { margin-bottom: 5px; }
input[type="text"], input[type="number"] {
width: calc(100% - 20px);
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box; /* 包含padding和border在内的宽度 */
}
button {
padding: 12px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
display: block;
width: 100%;
max-width: 200px;
margin: 0 auto 20px auto;
}
button:hover { background-color: #45a049; }
.result-section {
background-color: #e8f5e9;
border: 1px solid #c8e6c9;
padding: 15px;
margin-top: 25px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.result-section p { margin: 8px 0; font-size: 1.1em; }
.result-section p span { font-weight: bold; color: #388e3c; }
#list {
background-color: #ffffff;
border: 1px solid #ddd;
padding: 15px;
margin-top: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
#list h3 { color: #2c3e50; margin-top: 0; border-bottom: 1px solid #eee; padding-bottom: 10px; margin-bottom: 15px; }
#list p { margin: 8px 0; padding-left: 10px; border-left: 3px solid #66bb6a; }
</style>
</head>
<body>
<h1>费用分摊应用</h1>
<p>请输入每位参与者的姓名和消费金额:</p>
<p>姓名</p>
<input type="text" id="nombre" placeholder="例如:张三">
<br>
<p>金额</p>
<input type="number" id="monto" placeholder="例如:100">
<br>
<button onclick="ir()">添加/更新记录</button>
<br>
<div class="result-section">
<p>总支出: <span id="final">0</span> 元</p>
<p>参与人数: <span id="aporte">0</span> 人</p>
<p id="average">人均分摊: 0 元</p>
</div>
<div id="list">
<h3>消费明细:</h3>
<!-- 消费记录将在此处动态显示 -->
</div>
<script src="script.js"></script>
</body>
</html>HTML元素说明:
以上就是JavaScript动态费用分摊与人均计算应用开发教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号