【问题标题】:When I click on a value in the search bar list, I want it to go to the page当我单击搜索栏列表中的值时,我希望它转到页面
【发布时间】:2022-01-01 21:06:41
【问题描述】:

389 / 5.000 Çeviri sonuçları 您好,我的搜索框有问题。这个问题如下;

搜索框的工作逻辑基于 javascript 文件。单击字母“a”时,我想在选择其中任何一个时使用链接重定向到该页面。但该链接仅在将鼠标悬停在字母 a 上时可见。我不知道如何用不同的阵列拉出蛞蝓。如果您能提供帮助,我将不胜感激。

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("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items text-primary");
     
      this.parentNode.appendChild(a);
     
      for (i = 0; i < arr.length; i++) {
      
      
      
      
      
      
        var s1=arr[i].substr(0, val.length);
        var s2=val;
        
        
        var string = s1;
        var letters = { "İ": "i", "I": "ı", "Ş": "ş", "Ğ": "ğ", "Ü": "ü", "Ö": "ö", "Ç": "ç", "A": "a", "B": "b", "C": "c", "D": "d", "E": "e", "F": "f", "G": "g", "H": "h", "J": "j", "K": "k", "L": "l", "M": "m", "N": "n", "O": "o", "P": "p", "R": "r", "S": "s", "T": "t", "U": "u", "V": "v", "Y": "y", "Z": "z"};
        var s1 = string.replace(/(([İIŞĞÜÇÖABCDEFGHJKLMNOPRSTUVYZ]))/g, function(letter){ return letters[letter]; });
        
        
        var string = s2;
        var letters = { "İ": "i", "I": "ı", "Ş": "ş", "Ğ": "ğ", "Ü": "ü", "Ö": "ö", "Ç": "ç", "A": "a", "B": "b", "C": "c", "D": "d", "E": "e", "F": "f", "G": "g", "H": "h", "J": "j", "K": "k", "L": "l", "M": "m", "N": "n", "O": "o", "P": "p", "R": "r", "S": "s", "T": "t", "U": "u", "V": "v", "Y": "y", "Z": "z" };
        var s2 = string.replace(/(([İIŞĞÜÇÖABCDEFGHJKLMNOPRSTUVYZ]))/g, function(letter){ return letters[letter]; });
        

        
        if (s1 == s2) {
        
          b = document.createElement("DIV");
       
          b.innerHTML = '<a href="https://universitenitanit.com/">' + arr[i].substr(0, val.length) + '</a>';
          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) { //up
        
        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);
  });
}
var cities = ["İstanbul","Albania","Algeria","Isparta","Şırnak","Çanakkale","Denizli","Bitlis","Düzce","Ankara","Adana","Eskişehir","İstanbul Teknik Üniversitesi","Marmara Üniversitesi"];

autocomplete(document.getElementById("searchInput"), cities);
<input type="text" class="form-control form-input"
                id="searchInput" placeholder="Şehir veya Üniversite Ara...">

【问题讨论】:

  • 欢迎来到 Stack Overflow。你看过 jQuery UI 自动完成吗?它有一个onSelect 方法可能会有所帮助。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: javascript html jquery arrays search


【解决方案1】:

不清楚你在问什么。听起来您想在将鼠标悬停在链接上时显示工具提示?

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("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items text-primary");
     
      this.parentNode.appendChild(a);
     
      for (i = 0; i < arr.length; i++) {
      
      
      
      
      
      
        var s1=arr[i].substr(0, val.length);
        var s2=val;
        
        
        var string = s1;
        var letters = { "İ": "i", "I": "ı", "Ş": "ş", "Ğ": "ğ", "Ü": "ü", "Ö": "ö", "Ç": "ç", "A": "a", "B": "b", "C": "c", "D": "d", "E": "e", "F": "f", "G": "g", "H": "h", "J": "j", "K": "k", "L": "l", "M": "m", "N": "n", "O": "o", "P": "p", "R": "r", "S": "s", "T": "t", "U": "u", "V": "v", "Y": "y", "Z": "z"};
        var s1 = string.replace(/(([İIŞĞÜÇÖABCDEFGHJKLMNOPRSTUVYZ]))/g, function(letter){ return letters[letter]; });
        
        
        var string = s2;
        var letters = { "İ": "i", "I": "ı", "Ş": "ş", "Ğ": "ğ", "Ü": "ü", "Ö": "ö", "Ç": "ç", "A": "a", "B": "b", "C": "c", "D": "d", "E": "e", "F": "f", "G": "g", "H": "h", "J": "j", "K": "k", "L": "l", "M": "m", "N": "n", "O": "o", "P": "p", "R": "r", "S": "s", "T": "t", "U": "u", "V": "v", "Y": "y", "Z": "z" };
        var s2 = string.replace(/(([İIŞĞÜÇÖABCDEFGHJKLMNOPRSTUVYZ]))/g, function(letter){ return letters[letter]; });
        

        
        if (s1 == s2) {
        
          b = document.createElement("DIV");
       
          b.innerHTML = '<a class="tooltip" href="https://universitenitanit.com/">' + arr[i].substr(0, val.length) + '<span class="tooltipText">https://universitenitanit.com/</span></a>';
          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) { //up
        
        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);
  });
}
var cities = ["İstanbul","Albania","Algeria","Isparta","Şırnak","Çanakkale","Denizli","Bitlis","Düzce","Ankara","Adana","Eskişehir","İstanbul Teknik Üniversitesi","Marmara Üniversitesi"];

autocomplete(document.getElementById("searchInput"), cities);
.tooltip{
  position: relative;
}
.tooltip .tooltipText {
  visibility: hidden;
  position: absolute;
  z-index: 1;
  left: 200px;
}
.tooltip:hover .tooltipText {
  visibility: visible;
}
<input type="text" class="form-control form-input"
                id="searchInput" placeholder="Şehir veya Üniversite Ara...">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多