英文:
Autocomplete in the Middle of text
问题
我想要一个自动完成列表,它也可以在文本中间工作。我正在使用来自W3Schools的自动完成功能。
例如,自动完成的项目是:马拉维,马来西亚,墨西哥。我的目标是,即使我在输入框中键入“exi”,也能显示墨西哥。现在,我必须键入第一个M才能考虑墨西哥。
关于这个问题有很多使用jQuery的主题,但我希望使用纯JavaScript实现这个功能。
function autocomplete(inp, arr) {
/*自动完成函数接受两个参数,
文本字段元素和可能自动完成的值数组:*/
var currentFocus;
/*当有人在文本字段中输入时执行一个函数:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*关闭任何已经打开的自动完成值列表*/
closeAllLists();
if (!val) {
return false;
}
currentFocus = -1;
/*创建一个包含项目(值)的DIV元素:*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*将DIV元素附加为自动完成容器的子元素:*/
this.parentNode.appendChild(a);
/*对于数组中的每个项目...*/
for (i = 0; i < arr.length; i++) {
/*检查项目是否以与文本字段值相同的字母开头:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*为每个匹配的元素创建一个DIV元素:*/
b = document.createElement("DIV");
/*使匹配的字母加粗:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*插入一个输入字段,该字段将保存当前数组项的值:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*当有人点击项目值(DIV元素)时执行一个函数:*/
b.addEventListener("click", function(e) {
/*将值插入自动完成文本字段:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*关闭自动完成值列表,
(或任何其他已打开的自动完成值列表):*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*在键盘上按键时执行一个函数:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*如果按下箭头DOWN键,
增加currentFocus变量:*/
currentFocus++;
/*并使当前项目更可见:*/
addActive(x);
} else if (e.keyCode == 38) { //上
/*如果按下箭头UP键,
减少currentFocus变量:*/
currentFocus--;
/*并使当前项目更可见:*/
addActive(x);
} else if (e.keyCode == 13) {
/*如果按下回车键,阻止表单提交,*/
e.preventDefault();
if (currentFocus > -1) {
/*并模拟对“active”项目的点击:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*一个将项目分类为“active”的函数:*/
if (!x) return false;
/*首先删除所有项目上的“active”类:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*添加类“autocomplete-active”:*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*一个从所有自动完成项目中删除“active”类的函数:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*关闭文档中的所有自动完成列表,
除了作为参数传递的一个:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*当有人点击文档时执行一个函数:*/
document.addEventListener("click", function(e) {
closeAllLists(e.target);
});
}
/*包含世界上所有国家名称的数组:*/
var countries = ["阿富汗", "阿尔巴尼亚", "阿尔及利亚", "安道尔", "安哥拉", "安圭拉", "安提瓜和巴布达", "阿根廷", "亚美尼亚", "阿鲁巴", "澳大利亚", "奥地利", "阿塞拜疆", "巴哈马", "巴林", "孟加拉国", "巴巴多斯", "白俄罗斯", "比利时", "伯利兹", "贝宁", "百慕大", "不丹", "玻利维亚", "波斯尼亚和黑塞哥维那", "博茨瓦纳", "巴西", "英属维尔京群岛", "文莱", "保加利亚", "布基纳法索", "布隆迪", "柬埔寨", "喀麦隆", "加拿大", "佛得角", "开曼群岛", "中非共和国", "乍得", "智利", "中国", "哥伦比亚", "刚果", "库克群岛", "哥斯达黎加", "科特
<details>
<summary>英文:</summary>
I would like to have autocomplete list which works also in the middle of the text. I am using Autocomplete from W3Schools.
For example autocomplete items are: *Malawi, Malaysia, Mexico*. My goal is to have diplayed Mexico, even when I type "exi" into the input. Now I have to type the first M to even get Mexico into account.
There are plenty of topics about this with jQuery, but I would like to have that ability with vanilla JavaScript.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) {
return false;
}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function(e) {
closeAllLists(e.target);
});
}
/*An array containing all the country names in the world:*/
var countries = ["Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua & Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia & Herzegovina", "Botswana", "Brazil", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central Arfrican Republic", "Chad", "Chile", "China", "Colombia", "Congo", "Cook Islands", "Costa Rica", "Cote D Ivoire", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Polynesia", "French West Indies", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauro", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "North Korea", "Norway", "Oman", "Pakistan", "Palau", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Saint Pierre & Miquelon", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Korea", "South Sudan", "Spain", "Sri Lanka", "St Kitts & Nevis", "St Lucia", "St Vincent", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor L'Este", "Togo", "Tonga", "Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks & Caicos", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States of America", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Virgin Islands (US)", "Yemen", "Zambia", "Zimbabwe"];
/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
autocomplete(document.getElementById("myInput"), countries);
<!-- language: lang-html -->
<!--Make sure the form has the autocomplete function switched off:-->
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
<!-- end snippet -->
</details>
# 答案1
**得分**: 2
将以下代码行:
```javascript
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase())
更改为:
if (arr[i].toUpperCase().includes(val.toUpperCase()))
英文:
All you need to change the following:
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase())
To:
if (arr[i].toUpperCase().includes(val.toUpperCase()))
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].toUpperCase().includes(val.toUpperCase())) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
/*An array containing all the country names in the world:*/
var countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central Arfrican Republic","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauro","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"];
/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
autocomplete(document.getElementById("myInput"), countries);
<!-- language: lang-html -->
<h2>Autocomplete</h2>
<p>Start typing:</p>
<!--Make sure the form has the autocomplete function switched off:-->
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
<!-- end snippet -->
答案2
得分: 1
以下是您要翻译的内容:
"Using filter and refactoring most of the W3Schools out of the code, saving 31 lines in the process.
const autocomplete = (inp, arr) => {
let currentFocus = -1;
const closeAllLists = (elmnt) => document.querySelectorAll(".autocomplete-items").forEach((x) => elmnt != x && elmnt !== inp && x.remove());
const removeActive = (x) => x.forEach((item) => item.classList.remove("autocomplete-active"));
const addActive = (x) => {
if (!x) return;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
};
inp.addEventListener("input", e => {
currentFocus = -1;
let val = inp.value.toUpperCase();
closeAllLists();
if (!val) return;
let autocompleteList = arr
.filter(item => item.toUpperCase().includes(val))
.map(item => <div><strong>${item.substr(0, val.length)}</strong>${item.substr(val.length)}<input type='hidden' value='${item}'></div>
)
.join('');
const autocompleteDiv = <div id="${inp.id}autocomplete-list" class="autocomplete-items">${autocompleteList}</div>
;
inp.closest("div.autocomplete").insertAdjacentHTML('beforeend', autocompleteDiv);
});
inp.addEventListener("keydown", e => {
console.log(e.key, e.keyCode, currentFocus)
let x = inp.nextElementSibling;
if (x) x = x.querySelectorAll("div");
if (e.key === 'Enter') {
e.preventDefault();
if (currentFocus > -1 && x) x[currentFocus].click();
return;
}
if (e.key === 'ArrowDown') currentFocus++;
else if (e.key === 'ArrowUp') currentFocus--;
addActive(x);
});
document.addEventListener("click", e => {
const tgt = e.target;
if (tgt.matches("#myInputautocomplete-list div")) {
inp.value = e.target.querySelector("input").value;
closeAllLists();
} else {
closeAllLists(tgt);
}
});
};
const countries = ["Afghanistan", "Albania", "Mexico", "Zimbabwe"];
autocomplete(document.getElementById("myInput"), countries);
- {
box-sizing: border-box;
}
body {
font: 16px Arial;
}
/the container must be positioned relative:/
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 10px;
font-size: 16px;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
cursor: pointer;
}
.autocomplete-items {
position: absolute;
border: 1px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/position the autocomplete items to be the same width as the container:/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
/when hovering an item:/
.autocomplete-items div:hover {
background-color: #e9e9e9;
}
/when navigating through the items using the arrow keys:/
.autocomplete-active {
background-color: DodgerBlue !important;
color: #ffffff;
}
"
英文:
Using filter and refactoring most of the W3Schools out of the code, saving 31 lines in the process.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const autocomplete = (inp, arr) => {
let currentFocus = -1;
const closeAllLists = (elmnt) => document.querySelectorAll(".autocomplete-items").forEach((x) => elmnt != x && elmnt !== inp && x.remove());
const removeActive = (x) => x.forEach((item) => item.classList.remove("autocomplete-active"));
const addActive = (x) => {
if (!x) return;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
};
inp.addEventListener("input", e => {
currentFocus = -1;
let val = inp.value.toUpperCase();
closeAllLists();
if (!val) return;
let autocompleteList = arr
.filter(item => item.toUpperCase().includes(val))
.map(item => `<div><strong>${item.substr(0, val.length)}</strong>${item.substr(val.length)}<input type='hidden' value='${item}'></div>`)
.join('');
const autocompleteDiv = `<div id="${inp.id}autocomplete-list" class="autocomplete-items">${autocompleteList}</div>`;
inp.closest("div.autocomplete").insertAdjacentHTML('beforeend', autocompleteDiv);
});
inp.addEventListener("keydown", e => {
console.log(e.key, e.keyCode, currentFocus)
let x = inp.nextElementSibling;
if (x) x = x.querySelectorAll("div");
if (e.key === 'Enter') {
e.preventDefault();
if (currentFocus > -1 && x) x[currentFocus].click();
return;
}
if (e.key === 'ArrowDown') currentFocus++;
else if (e.key === 'ArrowUp') currentFocus--;
addActive(x);
});
document.addEventListener("click", e => {
const tgt = e.target;
if (tgt.matches("#myInputautocomplete-list div")) {
inp.value = e.target.querySelector("input").value;
closeAllLists();
} else {
closeAllLists(tgt);
}
});
};
const countries = ["Afghanistan", "Albania", "Mexico", "Zimbabwe"];
autocomplete(document.getElementById("myInput"), countries);
<!-- language: lang-css -->
* {
box-sizing: border-box;
}
body {
font: 16px Arial;
}
/*the container must be positioned relative:*/
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 10px;
font-size: 16px;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
cursor: pointer;
}
.autocomplete-items {
position: absolute;
border: 1px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
/*when hovering an item:*/
.autocomplete-items div:hover {
background-color: #e9e9e9;
}
/*when navigating through the items using the arrow keys:*/
.autocomplete-active {
background-color: DodgerBlue !important;
color: #ffffff;
}
<!-- language: lang-html -->
<!--Make sure the form has the autocomplete function switched off:-->
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论