簡體   English   中英

我如何將 javascript 數組中的單詞變成鏈接

[英]How can i make words in my javascript array into a link

我有一個自動填充單詞的搜索欄,我想制作自動填充的單詞鏈接。 所以我有一個名為 page2.html 的頁面,我想要它,這樣如果我輸入位置,它將自動填充單詞位置 en 當我單擊自動填充的單詞時,它將引導我到我的 Page2.html。

如果我 go 到我的搜索欄並輸入 Locations 我什么也得不到,如果我輸入“<”我得到代碼。 它不會使它成為一個鏈接。

這是我的 html/javascript,有一個針對底部箭頭的代碼:

  <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 a = document.createElement("a");
a.href = "page2.html";
a.innerHTML = arr[valIndex];

container.appendChild(a) // add it to the dom; container == the element the suggestion should be put in

其中arr[valIndex]是數組中的建議項。 如果您需要整個代碼,請在評論中告訴我;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM