javascript - 如何在变量前面键入文本
问题描述
我会尽力解释这一点。我正在制作一个域检查器,它对我有用,现在我有一个函数可以显示我在我的 var 中名为“domains”的一些域,一旦我输入一个点,因为所有变量域都以点开头域会出现,但是当我在放置 DOT 之前写一些文本时,它将不再出现。我必须更改什么才能让它在 DOT 之前显示文本?这是我使用的代码,以及一些图片现在的样子。
<body>
<center>
<h2>Autocomplete domein</h2>
<p>Start typing:</p>
<form autocomplete="off" action="#">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Domain">
</div>
<input type="submit">
</form>
</center>
<script>
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;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
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] + "'>";
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) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) {
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 1; 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 domain = [".nl",".com",".net",".org",".network",".at",".co",".co.uk"
,".be",".bl",".us",".eu",".uk", ".lu"];
autocomplete(document.getElementById("myInput"), domain);
</script>
</body>
解决方案
尝试
<body>
<center>
<h2>Autocomplete domein</h2>
<p>Start typing:</p>
<form autocomplete="off" action="#">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Domain">
</div>
<input type="submit">
</form>
</center>
<script>
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
var name ='';
if(this.value.indexOf('.')>=0){
val =this.value.slice(this.value.indexOf('.'));
name = this.value.slice(0,this.value.indexOf('.'));
}
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = name;
b.innerHTML += "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + name + arr[i] + "'>";
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) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) {
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 1; 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 domain = [".nl",".com",".net",".org",".network",".at",".co",".co.uk"
,".be",".bl",".us",".eu",".uk", ".lu"];
autocomplete(document.getElementById("myInput"), domain);
</script>
</body>
推荐阅读
- c# - 在 WaitForExit() 之前打开一个新的 WPF 窗口而不使窗口卡住
- magento2 - Magento 2获取商店ID
- c# - 如何在触发器中使用左边距值?
- azure-devops - 更改分配给 VSTS 中用户的任务的顺序
- android - Volley 网络请求响应没有 HTML 标签
- android - 如何在不使用 TabLout 中的向上按钮的情况下访问父活动
- visual-studio - 如何在无人值守模式下禁用 Visual Studio 扩展自动更新?
- javascript - 提交表单后重定向
- javascript - 在 2 个字段中拆分 Javascript 中的字符串
- python - 在 Python 中解析 DN、来自 LDAP URL 的属性