gpt4 book ai didi

javascript - 我怎样才能把我的javascript数组中的单词变成一个链接

转载 作者:行者123 更新时间:2023-12-05 05:51:20 24 4
gpt4 key购买 nike

我有一个自动填充单词的搜索栏,我想制作自动填充单词的链接。所以我有一个名为 page2.html 的页面,我想要它,这样如果我键入位置,它会自动填充单词 locations en 当我单击自动填充的单词时,它会引导我到我的 Page2.html。

如果我转到我的搜索栏并输入 Locations,我什么也得不到,如果我输入“<”,我会得到代码。它不会使其成为链接。

这是我的 html/javascript,底部有一段代码是针对 arry 的:

  <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>

最佳答案

您可以使用 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>

您可以在数组中设置目的地。我将它设置为两个演示站点,每个人都可以访问。

关于javascript - 我怎样才能把我的javascript数组中的单词变成一个链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70393630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com