我正在使用这个W3C Schools 自动完成下拉菜单。
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] + "'>";
});
a.appendChild(b);
}
}
});
我对这个自动完成功能中的这行代码感到困惑:
inp.value = this.getElementsByTagName("input")[0].value;
this上面的代码指的是什么?
我们为什么要这样做this.getElementsByTagName("input")[0]?
我正进入(状态:
未捕获的类型错误:无法读取未定义的属性“值”
在这一行。然而,我确实修改了上面的函数并删除了inp参数上的添加事件侦听器行。
慕慕森
慕婉清6462132
四季花海
相关分类