我有一個自動填充單詞的搜索欄,我想制作自動填充的單詞鏈接。所以我有一個名為 page2.html 的頁面,我想要它,這樣如果我輸入位置,它會自動填充單詞位置 en,當我點擊自動填充的詞時,它會將我定向到我的 Page2.html。
如果我去我的搜索欄并輸入 Locations 我什么也得不到,如果我輸入“<”我得到代碼。它并沒有使它成為一個鏈接。
這是我的 html/javascript,有一段代碼針對底部的 arry down:
<form autocomplete="off" action="/action_page.php">
<div class="SearchBar">
<input id="myInput" type="text" name="Search2" placeholder="Search...">
</div>
</form>
<script>
var SW = ["<a href='Page2.html'>Locations</a>"];
function SearchBar(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 "SearchBar-list");
a.setAttribute("class", "SearchBar-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 "SearchBar-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("SearchBar-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i ) {
x[i].classList.remove("SearchBar-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("SearchBar-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);
});
}
</script>
<script>
SearchBar(document.getElementById("myInput"), SW);
</script>
uj5u.com熱心網友回復:
您可以使用 來創建建議項document.createElement。
像這樣:
var SW = ["Locations", "test"];
var links = ["https://shadowlp174.4lima.de","https://stackoverflow.com"];
function SearchBar(inp, arr, links) {
var currentFocus;
inp.addEventListener("input", function(e) {
var div, b, i, val = this.value;
closeAllLists();
if (!val) {
return false;
}
currentFocus = -1;
div = document.createElement("div");
div.id = this.id "SearchBar-list";
div.classList.add("SearchBar-items");
this.parentNode.appendChild(div);
for (i = 0; i < arr.length; i ) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
var a = document.createElement("a");
a.href = links[i];
a.setAttribute("suggestion", arr[i]);
div.appendChild(a);
let strong = document.createElement("strong");
strong.innerHTML = arr[i].substr(0, val.length);
let span = document.createElement("span");
span.innerHTML = arr[i].substr(val.length, arr[i].length - 1);
a.appendChild(strong);
a.appendChild(span);
a.addEventListener("click", function(e) {
inp.value = this.getAttribute("suggestion");
closeAllLists();
});
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id "SearchBar-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("SearchBar-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i ) {
x[i].classList.remove("SearchBar-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("SearchBar-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);
});
}
SearchBar(document.getElementById("myInput"), SW, links);
<form autocomplete="off" action="/action_page.php">
<div class="SearchBar">
<input id="myInput" type="text" name="Search2" placeholder="Search...">
</div>
</form>
您可以在陣列中設定目的地。我將它設定為每個人都可以使用的兩個演示站點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/384469.html
標籤:javascript html css
上一篇:如何在沒有div的情況下操作文本(HTML/CSS)?
下一篇:根據輸入值隱藏嵌套按鈕
