UI AutoComplete(自动完成)是一种用户界面组件,它根据用户输入的文本动态显示匹配的建议列表。这种功能常见于搜索框、输入框等场景,旨在提高用户输入效率和准确性。
原因:
解决方法:
原因:
解决方法:
以下是一个简单的基于JavaScript和HTML的自动完成示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AutoComplete Example</title>
<style>
.suggestions {
width: 300px;
border: 1px solid #ccc;
background-color: white;
}
.suggestions div {
padding: 5px;
cursor: pointer;
}
.suggestions div:hover {
background-color: #eee;
}
</style>
</head>
<body>
<input type="text" id="autocomplete" placeholder="Type something...">
<div id="suggestions" class="suggestions"></div>
<script>
const data = ["Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape"];
const input = document.getElementById('autocomplete');
const suggestions = document.getElementById('suggestions');
input.addEventListener('input', function() {
const value = input.value.toLowerCase();
suggestions.innerHTML = '';
if (value.length > 0) {
const filteredData = data.filter(item => item.toLowerCase().includes(value));
filteredData.forEach(item => {
const div = document.createElement('div');
div.textContent = item;
div.addEventListener('click', function() {
input.value = item;
suggestions.innerHTML = '';
});
suggestions.appendChild(div);
});
}
});
</script>
</body>
</html>
希望这些信息对你有所帮助!如果有更多问题,请随时提问。
领取专属 10元无门槛券
手把手带您无忧上云