要创建一个包含“添加产品”按钮和一个空按钮的购物清单,你可以使用HTML、CSS和JavaScript来实现。以下是一个简单的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>购物清单</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="shopping-list">
<h1>购物清单</h1>
<ul id="itemList">
<!-- 产品列表将动态添加到这里 -->
</ul>
<input type="text" id="itemInput" placeholder="输入产品名称">
<button id="addItem">添加产品</button>
<button id="clearList">清空清单</button>
</div>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
}
.shopping-list {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin: 5px 0;
padding: 5px;
background-color: #f9f9f9;
border: 1px solid #ddd;
}
button {
margin-top: 10px;
}
document.getElementById('addItem').addEventListener('click', function() {
const input = document.getElementById('itemInput');
const itemList = document.getElementById('itemList');
const newItem = document.createElement('li');
newItem.textContent = input.value;
itemList.appendChild(newItem);
input.value = ''; // 清空输入框
});
document.getElementById('clearList').addEventListener('click', function() {
const itemList = document.getElementById('itemList');
itemList.innerHTML = ''; // 清空列表
});
这种简单的购物清单应用非常适合个人使用,或者在小型团队中共享购物需求。它可以轻松地扩展以包括更多功能,如编辑和删除列表项,或者将清单同步到云端。
通过这种方式,你可以创建一个基本的购物清单应用,并根据需要进行扩展和改进。
领取专属 10元无门槛券
手把手带您无忧上云