问题描述
当用户在输入框中输入国家名称时,页面会显示相应国家的列表。单击建议的国家会替换输入框中的值。为简单起见,仅考虑以"A" 开头的国家。
HTML:
<html>
<head>
</head>
<body>
<label for="country">Enter a country name</label>:
<input type="text" id="country">
<div id="suggestions"></div>
</body>
</html>
CSS:
/* Add spacing between each country suggestion */
.suggestion {
padding-left: 2px;
padding-right: 2px;
}
/* Change suggestion color when hovering it with the mouse */
.suggestion:hover {
background-color: #adf;
cursor: pointer;
}
/* Position the suggestion list just below the input box */
#suggestions {
position: absolute;
border: 1px solid black;
left: 155px;
}
显示效果:
解决方法
我的:必须在输入完点击别的地方才能显示出来,也就是失去焦点之后。。。。
解决,监听事件类型改为 "input" 即可
// Country list
const countryList = [
"Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Anguilla",
"Antarctica",
"Antigua-and-Barbuda",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Autria",
"Azerbaïjan"
];
const countryElement = document.getElementById("country");
const suggetsElement = document.getElementById("suggestions");
const createParaElement = text=>{
const element = document.createElement("p");
element.textContent = text;
return element;
}
countryElement.addEventListener("change", e=>{
if(e.target.value.startsWith("A"))
{
countryList.forEach(country =>{
suggetsElement.appendChild(createParaElement(country));
});
}
});
suggetsElement.addEventListener("click", e=>{
countryElement.value = e.target.textContent;
document.body.removeChild(suggetsElement);
});
显示效果:
参考答案:输入框以输入就显示联想建议
// Country list
const countryList = [
"Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Anguilla",
"Antarctica",
"Antigua-and-Barbuda",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Autria",
"Azerbaïjan"
];
const countryElement = document.querySelector("input");
const suggestionsElement = document.getElementById("suggestions");
// Create and return a suggestion
const createSuggestionElement = country => {
const element = document.createElement("div");
element.classList.add("suggestion");
element.textContent = country;
// Handle click on a suggestion
element.addEventListener("click", e => {
// Replace input with suggested country
countryElement.value = e.target.textContent;
// Empty the suggestion list
suggestionsElement.innerHTML = "";
});
return element;
};
// Handle input change
countryElement.addEventListener("input", () => {
// Empty suggestion list
suggestionsElement.innerHTML = "";
countryList.forEach(country => {
// Check if the inputted value matches the start of the country
if (country.startsWith(countryElement.value)) {
// Add the country as suggestion
suggestionsElement.appendChild(createSuggestionElement(country));
}
});
});
看了答案之后的改进:
// Country list
const countryList = [
"Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Anguilla",
"Antarctica",
"Antigua-and-Barbuda",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Autria",
"Azerbaïjan"
];
const countryElement = document.getElementById("country");
const suggetsElement = document.getElementById("suggestions");
const createParaElement = text=>{
const element = document.createElement("div");
element.classList.add("suggestion");
element.textContent = text;
element.addEventListener("click",e=>
{
countryElement.value=e.target.textContent;
suggetsElement.innerHTML="";
});
return element;
}
countryElement.addEventListener("input", e=>{
suggetsElement.innerHTML = "";
countryList.forEach(country =>
{
if(country.startsWith(e.target.value))
{
suggetsElement.appendChild(createParaElement(country));
}
});
});