如何将JavaScript数组中的单词转换为链接

3
我有一个搜索栏,可以自动填充单词,我想让这些自动填充的单词成为链接。所以我有一个名为page2.html的页面,如果我输入locations它会自动填充locations单词,并且当我点击这个自动填充的单词时,它会将我引导到我的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>
1个回答

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

您可以在数组中设置目标站点。我将其设置为两个演示站点,对所有人都可用。


非常感谢你。 - Thomas
很好,我很高兴能够帮助。 :) - ShadowLp174
1
我会编辑答案,这样你就有一个可用的示例了 @Thomas - ShadowLp174
我复制了你的示例,但是如果我在我的网站上打开并在搜索栏中键入“locations”或“test”,我没有得到任何建议,我真的不明白我需要替换什么。-_- - Thomas
哦,等等,我明白了,我替换错了。所有的建议都有效,并且它们都链接到我想要的地方。现在我有三个链接,它们都可以使用。你太棒了,谢谢! - Thomas
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接