如何将项目链接添加到要搜索的项目数组中



如何添加到我建议的项目数组中的每个项目的链接,一旦我进行搜索,我需要它带我到包含这些项目的页面。我已经使用vannila javascript编写了自动完成搜索

下面是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;
a = document.createElement("p");
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 = 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);
});
}
autocomplete(document.getElementById("myInput"), countries);

我在这里所做的是,当输入值时,它将与此连接并带我到所需的页面,但我不能这样做,我在想可能有另一种方式

document.getElementById('searchform').onsubmit = function() {
window.location = 'http://www.skyyonliquor.com' + "/" + document.getElementById('myInput').value +"-section.html";
return false;

my建议数组我需要添加链接到:

var countries = [ 
"Spirit","Whiskey","Rum","Vodka","Brandy & Cognac","Malibu","Tequila","Chateau Mouton Rothschild","Chateau Margaux","Baron Philippe de Rothschild Mouton Cadet","Dom perignon","Moet Imperial","Moet Nectar Imperial Rose","Veuve Clicquot Rose Demi Sec","Clase Azul","Barware","Vodka","Gin","Champagne","Hennessy V.S.O.P","Hennesy X.O","Martell Blue Swift","Martell VSOP","Johnnie Walker Blue Label","Jack Daniels Old No. 7","Jack Daniels Honey","Jameson Black Barrel","Black Velvet","Kavalan"];

我能得到帮助吗?

只需循环遍历数组并创建一个包含您想要的地址的AHTML元素,并将其附加到您想要显示它的元素。

var countries = [ 
"Spirit","Whiskey","Rum","Vodka","Brandy & Cognac","Malibu","Tequila","Chateau Mouton Rothschild","Chateau Margaux","Baron Philippe de Rothschild Mouton Cadet","Dom perignon","Moet Imperial","Moet Nectar Imperial Rose","Veuve Clicquot Rose Demi Sec","Clase Azul","Barware","Vodka","Gin","Champagne","Hennessy V.S.O.P","Hennesy X.O","Martell Blue Swift","Martell VSOP","Johnnie Walker Blue Label","Jack Daniels Old No. 7","Jack Daniels Honey","Jameson Black Barrel","Black Velvet","Kavalan"];
const parent = document.getElementById("myElement");
for(let i = 0; i < countries.length; i++)
{
const a = document.createElement("a");
a.textContent = countries[i]; //displayed text
a.href = "http://example.com/mysearch?q=" + countries[i] + "&myquery=blah"; //link
//a.className = "myLink"; //style if needed
parent.appendChild(a);
}
#myElement > a.myLink
{
text-decoration: none;
margin-right: 1em;
}
<div id="myElement"></div>

相关内容

  • 没有找到相关文章